If a method takes a parameter of type System.Collections.IList can I legitimately/safely pass a value of type System.Collections.Generic.IEnumerable<T>?
I would appreciate a thorough explanation of why this is possible and what actually happens to the object T when the IEnumerable<T> is used inside of the method.
Is it converted to the base type of Object?
Is it used as an System.Collections.IEnumerable?
Are there any scenarios where this will cause problems (i.e. performance issues)?
Thanks in advance,
John
No, You cannot pass an
IEnumerable<T>to a method which takesIList. The interfaceIEnumerable<T>is not convertible toIListand attempting to pass a variable of typeIEnumerable<T>to a method takingIListwill result in a compilation error.In order to make this work you will need to do one of the following
IEnumerable<T>, maintain a reference to a type which implements bothIEnumerable<T>andIList.List<T>is a good candidate hereIEnumerable<T>instance to a new object which is convertible toIList. For example, in 3.5+ you can call the.ToList()extension method to create a newList<T>over the enumeration.