Click here to Skip to main content
15,868,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Difference b/w creating the instance over IList and List

Ex ::

IList<Customer> objIlistCust = new List<Customer>();  
List<Customer> objListCustomer = new List<Customer>();
Posted
Updated 6-Mar-14 0:28am
v2
Comments
johannesnestler 6-Mar-14 10:09am    
This is not a "bad" question or so, but If I were you, I would pick up my C# book again and read on interfaces - I think, if you have such basic understanding Problems, there is not much to learn from the "pro"-answers given... just my 2cents

There is no difference in the created instance - that is the right hand side of the equals - it's the variable on the left that is different. Because the variable is reference to the interface rather than the concrete class, you can only compile statements that use the interface properties, methods, and fields - you cannot use any methods with are specific to the List<T> class despite the instance the variable actually refers to being a concrete List<T> object.
This is because as far as the compiler is concerned, the variable may contain any class that implements the IList interface, and not all of them will be List class instances.

It's like saying a Car is a Vehicle:
C#
Vehicle v = new Car("Ford", "Mustang");
When you use the Vehicle next time, you can't even depend on the number of wheels, because I could also say:
C#
v = new Motorcycle("Yamaha", "R1");

If I say:
C#
Car c = new Car("Ford", "Fiesta");
Then I can depend on the wheel count, because the compiler will complain if I try to say:
C#
c = new Motorcycle("Honda", "Fireblade");
Because it knows that a Motorcycle is not a Car.
 
Share this answer
 
Nice Explanation by Lee

There are two rules I follow:

Accept the most basic type that will work
Return the richest type your user will need

So when writing a function or method that takes a collection, write it not to take a List, but an IList<T>, an ICollection<T>, or IEnumerable<T>. The generic interfaces will still work even for heterogenous lists because System.Object can be a T too. Doing this will save you headache if you decide to use a Stack or some other data structure further down the road. If all you need to do in the function is foreach through it, IEnumerable<T> is really all you should be asking for.

On the other hand, when returning an object out of a function, you want to give the user the richest possible set of operations without them having to cast around. So in that case, if it's a List<T> internally, return a copy as a List<T>.


Reference :
http://stackoverflow.com/questions/17170/when-to-use-ilist-and-when-to-use-list[^]
 
Share this answer
 
v3
Comments
Ankur\m/ 6-Mar-14 6:48am    
Please do mention that you have copied the answer from the link.
Tejas Vaishnav 6-Mar-14 8:10am    
I have mention that, "Nice Explanation By Lee", then at the end i have also copied stack overflow link from where i got that explanation..
Ankur\m/ 7-Mar-14 2:06am    
I never realized it's a copy-paste from the mentioned site until I checked the link. That is why I mentioned it.
Tejas Vaishnav 7-Mar-14 3:24am    
I have modified my answers, now is this correct representation or not.. please let me know, I like to here from you, its really helpful to me in future to correct my self, so i will careful next time.
Ankur\m/ 7-Mar-14 4:15am    
It's fine now. Thank you for taking the feedback positively! :thumbsup:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900