Click here to Skip to main content
15,888,212 members
Please Sign up or sign in to vote.
1.86/5 (6 votes)
See more:
Can anybody clearly understand me about ilist and list.Please give me some example so that i can cleary understand.

thank you .
Posted

Hi!

Just want to add a few things...

The idea is to program against the interface, not the implementation. So IList is interface (contract) which is used to implement List. For example if you want to write a method that return and accept "collection" of objects, you should write your method to return and accept interfaces. What interface you want to use depends on want you want to do in that method.
For example, lets say if you just want to iterate over collection then you should use the "smallest" interface type which is IEnumerable, not concrete implementation (List, Collection, ArrayList, StringCollection and so on...). Returning and accepting defined interface you only guaranties or promising that you component, method or something will behave on certain way. Look this AddRange method that is defined on List class:
C#
public void AddRange (
    IEnumerable<t> collection
)</t>

Using this method you can add several items into given list instance. So look its definition closely. It is defined to accept IEnumerable. this way author of this method provided you solution that you can add any collection type into given list, because every collection have IEnumebrable implemented and its the "smallest" interface type which supports a simple iteration over a collection.

If you want to read more about how and for what are interfaces used, take a look at this book[^]...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Feb-12 15:23pm    
Good answer again; I agree and voted 5.
I also added some basic links and explanation about incorrectness of such questions, please see my answer.
--SA
Martin Arapovic 5-Feb-12 15:53pm    
Ok! Tnx...
Wonde Tadesse 5-Feb-12 15:24pm    
5+
Espen Harlinn 5-Feb-12 16:25pm    
5'ed!
Hi there,

So a List object allows you to create a list, add things to it, remove it, update it, index into it etc etc and that's all very good. List is used whenever you just want a generic List where you specify object type in it and that's it.

IList on the other hand is an Interface. (For more on interfaces see MSDN Interfaces[^]). Basically, if you want to create your own type of List, say a list class called SimpleList, then you can use the Interface to give you basic methods and structure to your new class. IList is for when you want to create your own, special sub-class that implements List.

For further reading see:
MSDN List[^]
MSDN IList[^] - specifically look at the example code and see the SimpleList class.

Hope this helps,
Ed
 
Share this answer
 
Comments
Wonde Tadesse 5-Feb-12 15:24pm    
5+
Ed Nutting 5-Feb-12 15:25pm    
Thank you :)
Espen Harlinn 5-Feb-12 16:24pm    
5'ed!
Ed Nutting 5-Feb-12 16:27pm    
Thanks :)
The difference is that IList is an Interface and cannot be instantiated. List is a class and can be instantiated.
Ie: you can't say
C#
IList<string> list = new IList<string>();
But you can say
C#
List<string> list = new List<string>


The means that your own class can derive from both, but with an IList interface you can derive from a "normal" class ("MyClass" for example) and IList:
You can say:
C#
public class MyClassList: MyListsClass, IList<MyClass>
But you can't say
C#
public class MyClassList: MyListsClass, List<MyClass>
Because that would require C# deriving from multiple classes, which it can't.
 
Share this answer
 
Comments
Wonde Tadesse 5-Feb-12 15:21pm    
5+
Espen Harlinn 5-Feb-12 16:25pm    
5'ed!
pjpdada 28-Feb-13 8:25am    
yes
Before asking questions, please read this discussion:
How to ask a good question?[^].

Also, check out my answer on related topic: what is the difference between the class and encapsulation in programming[^].

Instead of posing incorrect and useless questions about "difference", you need just elementary education in OOP and programming in general.

First, take a look here:
http://en.wikipedia.org/wiki/Object-oriented_programming[^],
http://en.wikipedia.org/wiki/Dynamic_dispatch[^],
http://en.wikipedia.org/wiki/Interface_%28computing%29#Software_Interfaces[^].

Good luck,
—SA
 
Share this answer
 
Comments
Martin Arapovic 5-Feb-12 16:05pm    
Good links for people that doesn't understand principles of reusable object-oriented design or OOP desing in general.
Sergey Alexandrovich Kryukov 19-Jun-12 2:37am    
Thank you, Martin.
(I paid attention for this old post because I noticed a new vote of 1. I wonder why two people could vote like that?)
--SA
Member 12811035 25-Dec-16 12:18pm    
don't think that u r genius...
Member 14548404 10-Sep-19 2:31am    
before answering questions, read this link www.f***you.com
IList<> is an Interface While List<> is an Concrete Class.

IList<> Can not be instantiated While List can be Instantiated.

For Example :
-----------------

I have a class named Employee like
C#
Public Class Employee
{
    //Member function
    //Member variables
}


Then Following is not possible
C#
IList<employee> EmpObj = new IList<employee>

While Following are possible
C#
IList<employee> EmpObj = new List<employee> 

OR
C#
List<employee> EmpObj = new List<employee></employee></employee></employee></employee></employee></employee>
 
Share this answer
 
v2
Comments
Vishnu Prajapati 29-Oct-13 8:26am    
why exactly need

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