1 svar
581 visningar
daygamer 15
Postad: 21 sep 2018 14:28

Framkalla List<Bok> med ToString metoden C#

private List<Bok> bokLista = new List<Bok>();

public Bibliotekarie(object bokLista)
{


Console.WriteLine("EXEMPEL " + bokLista + " TEXT");

}
public override string ToString()
{

return base.ToString() + "Roman///////////////////// " + bokLista.ToString();
}

 

Jag undrar hur jag skriver detta för att få ut korrekt utskrift av return base samt bokLista.ToString();?

Euclid 572
Postad: 12 mar 2019 09:14

using System;
using System.Collections.Generic;


public class Book
{
public string title;
public string author;


public override string ToString() {
return String.Format("{0}, {1}", author, title);
}
}


public class BookList<T> : List<T> where T : Book
{
public override string ToString() {
string result = String.Format("Author, Title\n");
foreach (Book b in this)
result += b + "\n";
return result;
}
}


public class Program
{
public static void Main()
{
BookList<Book> books = new BookList<Book>();
Book book;
 
book = new Book();
book.author = "Bob";
book.title = "Eating Bananas";
books.Add(book);
book = new Book();
book.author = "Will";
book.title = "Eating Oranges";
books.Add(book);
book = new Book();
book.author = "Steve";
book.title = "Eating Apples";
books.Add(book);


Console.WriteLine(books);
}
}

Svara
Close