Chris J.
Chris J.

Reputation: 3

How to display the index of element in a list

I'm new to and currently learning C#, and I have to write a console application where the user inputs a list of, let's say, books, but it's a list of classes. Let's say I have this class called Books.

class Books
{
    public string name;
    public string description;
    public double price;

And now I create a list with type Books

List<Books> myBooks = new List<Books>();

And that I ask the user to add the books:

for (int x = 0; x <= s; x++)
{
    Books newbook = new Books();
    Console.WriteLine("\nPlease input the name, description and price of the book:\n");
    newbook.name = Console.ReadLine();
    newbook.description = Console.ReadLine();
    newbook.price = Convert.ToDouble(Console.ReadLine());
    myBooks.Add(newbook);
    //Displaying what the user just entered
    Console.WriteLine("{0} - {1}: {2}. Price: {3}", /*add index*/, newbook.name, newbook.description, newbook.price);
}

As you can see, I needed something to display in which part of the list the book is in, for each one of them (books).

I tried using, myBooks.Count() and myBooks[x], but those returned either the same value (because of the size of the list) or just [namespace.class]. Is there a solution (in an integer form, it can be zero based too) that doesn't involve adding another class or creating another variable?

Thanks in advance.

Upvotes: 0

Views: 741

Answers (3)

Kirk Broadhurst
Kirk Broadhurst

Reputation: 28728

You'll use the IndexOf method on the list

myBooks.Add(newbook);
//Displaying what the user just entered
Console.WriteLine("{0} - {1}: {2}. Price: {3}", myBooks.IndexOf(newbook), 
     newbook.name, newbook.description, newbook.price);

I don't see why myBooks.Count() wouldn't also work, because you're inserting at the end of the list, but the most descriptive method is using IndexOf.

Side note: your class Books describes a book; general practice is name a class after a singular object. You might want to rename that to Book for simplicity or clarity.

Upvotes: 0

Fourat
Fourat

Reputation: 2447

If you want an indexed collection just use an array :

int totalBooks = 25;
Books[] myBooks = new Books[totalBooks]; // 25 is the number of books an the indexes are from 0 to 24
for (int i = 0; i < totalBooks; i++)
{
    Books newbook = new Books();
    Console.WriteLine("\nPlease input the name, description and price of the book {0} :\n", (i+1));
    newbook.name = Console.ReadLine();
    newbook.description = Console.ReadLine();
    newbook.price = Convert.ToDouble(Console.ReadLine());
    myBooks[i] = newbook;
    //Displaying what the user just entered
    Console.WriteLine("{0} - {1}: {2}. Price: {3}", (i+1), newbook.name, newbook.description, newbook.price);
}

Upvotes: 0

Jonathan Wood
Jonathan Wood

Reputation: 67283

To get the index of the most recently added item, you can use (myBooks.Count() - 1).

Alternatively, you could store the value returned from myBooks.Count() before you add the item, and that value will be the index where the item will be added.

Finally, in your test example, you could've also used the value of x.

Upvotes: 2

Related Questions