How do I check for the number of different indexes in a List?

Title.

I’m not sure to understand what you need…

say I had this list

The List {“Wood”,“Wood”,“Wood”,“Lemon”,“Orange”,Seed")

There is 4 indexes… yes?

Wood,Lemon,Orange,Seed … 4

so how would I check for that?

Nope that’s one index with six elements. The logic you use to get four is likely flawed.

By “index” I think he means “unique elements”.

with some linq, pretty easy to count unique values:

var c = myList.Distinct().Count();

But yeah, using the word “indexes” is a bit of a misnomer.

The index of an element is the position in the list it exists (usually 0-based, 0 being the first element).

The number of indices would be equal to the number of elements (unique or not).

Now if you called ‘IndexOf(“Wood”)’ on that list you’d likely get 0, and if you called ‘IndexOf(“Lemon”)’ you’d likely get 3. But that doesn’t mean that “Wood” only has 1 index. It’s just that the ‘IndexOf(…)’ method returns the index of the FIRST element found that matches. That’s why there is 3 overloads of the method on the List class:

one of which being IndexOf(T, int):

This one you can get the IndexOf an element starting at some index in the list. So you could say:

var i = myList.IndexOf("Wood");

//this will loop through all indices of "Wood"
while(i >= 0)
{
    //do something with i

    //get the NEXT index of "Wood"
    i = myList.Index("Wood", i + 1);
}

Questions:

Can you translate that into C#?
And is there something I need to load from system.collections?
Would c = 4?

that is C#

just make sure you declare ‘using System.Linq;’ at the top of your class

Great!