I have class that wants an IList<T>
, but I have a Systems.Collection.IList
, coming from an NHibernate quere.
I want to create a method that converts it to an IList<T>
. How do I do this?
Convert the IEnumerable<T> instance to a new object which is convertible to IList . For example, in 3.5+ you can call the . ToList() extension method to create a new List<T> over the enumeration.
The IList<T> generic interface is a descendant of the ICollection<T> generic interface and is the base interface of all generic lists.
IList Interface (System.
If you're sure that all of the elements inherit from T (or whatever type you're using)
IList<T> myList = nonGenericList.Cast<T>().ToList();
If you're not sure:
IList<T> myList = nonGenericList.OfType<T>().ToList();
Of course, you will need the System.Linq namespace:
using System.Linq;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With