Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is IList used for?

Tags:

c#

I have already searched for it on MSDN and I read the definition that it is a non generic .... but my problem is that i am not understanding what is its use and when to use it . So i hope that anyone can help me . Thanks .

like image 875
Sam Watkins Avatar asked Dec 18 '25 04:12

Sam Watkins


1 Answers

In addition to the older code that doesn't know about generics, there are also a lot of cases where you know you have a list of data, but you can't know of what ahead of time. Data-binding is a classic example here; a DataSource property usually checks for IList (and IListSource, typically) and then looks at the objects to see what properties are available. It can't use generics in this case.

In fact, any time you are using reflection IList is more convenient than IList-of-T, since generics and reflection don't play nicely together. It can be done, but it is a pain. Unfortunately since IList-of-T doesn't derive from IList there are cases where this can fail - but it is a good 95% rule.

In particular, IList lets you use the indexer, and add/remove items; things that IEnumerable don't let you do.

Most app-level code (i.e. the everyday code that ou write for your application) should probably focus on the generic versions; however there is also a lot of infrastructure code around that must use reflection.

like image 148
Marc Gravell Avatar answered Dec 20 '25 17:12

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!