C# Difference between List and IList

Can you please  tell me the  difference between

List and ILlsit in C#
N_SriAsked:
Who is Participating?
 
Shahan AyyubSenior Software Engineer - iOSCommented:
List is an object we can instantiate it. Also List<T> is also available that can instantiate an object of your mention datatypes.

While IList is an interface,we implements its methods & properties in our classes.

As mention by lazyberezovsky(In first example)  some signatures are provided in example. We implements such kinds of signatures in our classes by the help of inheritance.

Moreover IList is inheritance by ICollection & IEnumerable



-Shahan

0
 
lazyberezovskyCommented:
IList is interface for collection of objects that can be individually accessed by index.

public interface IList : ICollection, IEnumerable
{
    // Methods
    int Add(object value);
    void Clear();
    bool Contains(object value);
    int IndexOf(object value);
    void Insert(int index, object value);
    void Remove(object value);
    void RemoveAt(int index);

    // Properties
    bool IsFixedSize { get; }
    bool IsReadOnly { get; }
    object this[int index] { get; set; }
}

Many collection classes implement IList interface: List<T>, ArrayList, Array, TreeNodeCollection etc
So, List<T> is just implementation of generic version of IList interface and some other interfaces.

public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
{
 // Implementation goes here.
}
0
 
tikusbalapCommented:
IList is an interface. You can't instantiate an object from it. See: http://msdn.microsoft.com/en-us/library/87d83y5b%28VS.80%29.aspx

List is an object. You can instantiate an object from it.

IList and List can be used as polymorphism. IList<string> myList = new List<string>(); See: http://msdn.microsoft.com/en-us/library/ms173152%28VS.80%29.aspx

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.