What is the advantage of using IList over List in C# ?
-
Hi , What is the advantage of using IList over List in C# ? what is the difference between List and Ilist ? Thanks, Azeez
-
Hi , What is the advantage of using IList over List in C# ? what is the difference between List and Ilist ? Thanks, Azeez
If you take a normal
List<T>
and call it'sAsReadOnly
method then it returns anIList<T>
, so I assume an IList can be readonly whereas a List can't. I've never looked into it so I could be wrong... just an observation I made a couple of months ago and figured I'd investigate one day!Dave
Generic BackgroundWorker - My latest article!
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Hi , What is the advantage of using IList over List in C# ? what is the difference between List and Ilist ? Thanks, Azeez
IList is an interface from which all lists derive from. So, while List is an IList, the opposite is not true. If you need a normal list, use the List class, if you need to build your own list with custom behavior (such as read only, for example), derive from IList. If you want your method to accept as parameter different type of list from the standard one, use IList as parameter type. If you want to hide the specific list type your method is returning, use IList as return type.
-
Hi , What is the advantage of using IList over List in C# ? what is the difference between List and Ilist ? Thanks, Azeez
Flexibility. A method that accepts a List can only accept a List (or something derived from List), whereas a method that accepts an IList can accept anything that implements IList, not just List.