views:

65

answers:

2

Hi

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

+3  A: 

The type of the variable itself is irrelevant to the ElementAt method - as far as it's concerned, it's only declared as IEnumerable<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.

Jon Skeet
A: 

No, why there should be a difference. ElementAt() is an extension method for IEnumerable<T>. There is no polymorphism in this case.

DHN