The MSDN library entry to Enumerable.ElementAt(TSource) Method says
“If the type of source implements
IList, that implementation is used
to obtain the element at the specified
index. Otherwise, this method obtains
the specified element.”
Let’s say we have following example:
ICollection<int> col = new List<int>() { /* fill with items */ };
IList<int> list = new List<int>() { /* fill with items */ };
col.ElementAt(10000000);
list.ElementAt(10000000);
Is there any difference in execution? or does ElementAt recognize that col also implements IList<> although it’s only declared as ICollection<>?
Thanks
The type of the variable itself is irrelevant to the
ElementAtmethod – as far as it’s concerned, it’s only declared asIEnumerable<T>, because that’s what the parameter type is. (Both calls will be bound to the same extension method.)It’s the execution-time type of the object which is tested in
ElementAt.