Does IndexOf return the first index or Random?


Suppose I have the following List<String> (C#) List(Of String) (VB) defined:

MyList(0) = "a"
MyList(1) = "b"
MyList(2) = "c"
MyList(3) = "b"
MyList(4) = "b"
MyList(5) = "b"

And, in my code I have MyList.IndexOf("b") - Is it safe to assume it returns the first index (In this case 1), or could it return 1, 3, 4 or 5?

In other words, does the IndexOf function iterate through a list object sequentially or not?


Yes, it returns the first index, unless you provide a second parameter (an index) to determine where the search begins. There's even a third parameter which can be used to search through a range.