Note that there are some explanatory texts on larger screens.

plurals
  1. POprint out a list of books with a for loop, c#
    primarykey
    data
    text
    <p>I have a book class that holds a couple of books, with book author, publishing year and name of book. now I want to print out the books using a loop but im not sure what loop i should use. here is my code for the book class:</p> <pre><code>class Book { public string forfattareEfternamn; public string forfattareFornamn; public string bokensTittle; public int lanseringsDatum; public string BokensTittle { get { return bokensTittle; } set { bokensTittle = value; } } public string ForfattareFornamn { get {return forfattareFornamn;} set {forfattareFornamn = value;} } public string ForfattareEfternamn { get {return forfattareEfternamn;} set {forfattareEfternamn = value;;} } public int LanseringsDatum { get { return lanseringsDatum; } set { lanseringsDatum = value; } } public override string ToString() { return string.Format("{0}, {1}, {2}, {3} ", forfattareEfternamn, ForfattareFornamn, bokensTittle, lanseringsDatum); } } </code></pre> <p>and here is the code in the Main, where i have created my list of books:</p> <pre><code>class Program { static void Main(string[] args) { List&lt;Book&gt; books = new List&lt;Book&gt;(); books.Add(new Book { forfattareFornamn = "Dumas", forfattareEfternamn = "Alexandre", bokensTittle = "The Count Of Monte Cristo", lanseringsDatum = 1844 }); books.Add(new Book { forfattareFornamn = "Clark", forfattareEfternamn = "Arthur C", bokensTittle = "Rendezvous with Rama", lanseringsDatum = 1972 }); books.Add(new Book { forfattareFornamn = "Dumas", forfattareEfternamn = "Alexandre", bokensTittle = "The Three Musketeers", lanseringsDatum = 1844 }); books.Add(new Book { forfattareFornamn = "Defoe", forfattareEfternamn = "Daniel", bokensTittle = "Robinson Cruise", lanseringsDatum = 1719 }); books.Add(new Book { forfattareFornamn = "Clark", forfattareEfternamn = "Arthur C", bokensTittle = "2001: A space Odyssey", lanseringsDatum = 1968 }); } } </code></pre> <p>So, I want to use a loop that prints the books in the list on the console screen using the ToString() method (without calling ToString() explicitly)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload