When indexing into an IEnumerable, I've noticed that using an index that is out of range will result in a default value being returned. I would have expected to receive an ArgumentOutOfRangeException. For example, this code results in listItem being 0. No exception is thrown.
Dim list As IEnumerable = New List(Of Integer)({1, 2, 3})
Dim listItem As Integer = CInt(list(-1))
If I cast as an IList, I get the ArgumentOutOfRangeException as expected.
This code throws the exception.
Dim list As IList = New List(Of Integer)({1, 2, 3})
Dim listItem As Integer = CInt(list(-1))
The fact that I can index into IEnumerable is surprising in it of itself. I'm sure it's using Linq extensions, but I cannot figure out which is in use. The behavior seems to indicate the ElementAtOrDefault method is being used, but I cannot find any documentation to support this.
What's going on here? Is there a good way to figure out which methods are actually in use?