Conversion of IEnumerable<T> to IList
Solution 1
No, You cannot pass an IEnumerable<T>
to a method which takes IList
. The interface IEnumerable<T>
is not convertible to IList
and attempting to pass a variable of type IEnumerable<T>
to a method taking IList
will result in a compilation error.
In order to make this work you will need to do one of the following
- Instead of having an
IEnumerable<T>
, maintain a reference to a type which implements bothIEnumerable<T>
andIList
.List<T>
is a good candidate here - Convert the
IEnumerable<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.
Solution 2
You will have to call ToList()
.
http://msdn.microsoft.com/en-us/library/bb342261.aspx
Solution 3
No. You can't, without using the ToList()
extension method. I think that this shouldn't cause that big of a performance problem - if you have an alternative, time them and compare.
You could also use new List<T>(myIEnumerable)
- the documentation says that that function is an O(n)
operation, but I can't find anything else about ToList()
. Also, remember that List<T>
also implements IList
as well as IList<T>
.
Solution 4
IEnumerable does not allow indexed access to the collection, whereas IList does. Thus, IList is more than IEnumerable and you won't be able to get away with it. Of course, some classes implement both interfaces (List<> for example) and those will work fine.
If you need to go from IEnumerable to IList, you may try a cast first, if you get lucky it will work. If not, you'll have to foreach over the IEnumerable collection and build a new IList.
Edit: use ToList() instead of foreaching yourself as others have suggested.
Related videos on Youtube

John Maloney
John Maloney Billings, MT – 406.208.9921 – [email protected] – https://www.linkedin.com/in/johnpatrickmaloney/ - https://github.com/johnmaloney Skills Development Lifecycle Management Architectural Patterns Agile Methodologies Software Testing Methodologies Object Orientation Software Requirements Team Management Technical Troubleshooting Strategic IT Planning Data Management & Analysis Application Testing Quality Assurance IT Product Management About Accomplished and technically sophisticated IT leader with comprehensive experience in leading agile teams and mentoring for application development, as well as streamlining data analysis across all phases of the development life cycle. Strong history of delivering exceptional leadership and guidance to agile teams and developers with keen focus on building high-performing teams. Stellar record of leading cross functional teams, and spearheading complex software development projects with a keen focus on improving project outcomes/deliverables. Skilled team leader with a track record in system design and development, applications programming, and program management. Demonstrated success in planning and managing development of up-to-date, high-quality, and innovative software solutions. Accomplished communicator skilled in building and strengthening relationships across functions to drive cohesive, strategic operations. CAREER ACCOMPLISHMENTS Extended history of improving the quality and sustainability of large code bases through analysis and technical debt mitigation. Strong drive to continually learn and study to perform at a higher level including extensive reading and the creation of projects outside of professional deliverables. Established as a reliable and efficient team member, continually contributing to the team above and beyond expectations which has allowed for the establishment of a track record of high performing teams. Framework driven approach to the software development life cycle allowing projects to grow and extend without losing maintainability. PROFESSIONAL EXPERIENCE S&P GLOBAL ⎯ Remote 2018 to Present Associate Director Administered and managed developers and multiple projects to gain demonstrated leadership skills. Spearheaded and delivered complex Data Services projects to improve project outcomes, while strategically positioned as Lead developer and team manager. Delivered exceptional leadership and guidance to developers across multiple teams to support a large ecosystem of services and ensure continuous performance improvement. For further work history, education and certifications see my profile here.
Updated on July 09, 2022Comments
-
John Maloney over 1 year
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 theIEnumerable<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