>> What is List in C# with example ? >> What is IList in C# with example ? >> Difference Between List and IList in C#
What is List in C#
- List is a class that represents a list of objects which can be accessed by index.
- List is a class that implements various interfaces. The programmer can create an object of List<T> object and assign it to any of its interface type variables. Moreover, it is possible to create an object of List as follows.
List<int> intList = new List<int>();
What is IList in C#
- IList is an interface that represents a collection of objects which can be accessed by index.
- List<T> is a concrete implementation of IList<T> interface. In OOP, it is a good programming practice to use interfaces than using concrete classes. Therefore, the programmer can use IList>T> type variable to create an object of List<T>, It is possible to create a IList object as follows
IList<int> intList = new List>int>();
public class Program
{
static void Main(string[] args)
{
List<int> intlist = new List<int>();
intlist.Add(1);
intlist.Add(2);
intlist.Add(3);
intlist.Add(4);
intlist.Add(5);
intlist.Add(6);
intlist.Add(7);
intlist.RemoveAt(5);
foreach (var num in intlist) {
Console.WriteLine(num + " ");
}
Console.ReadLine();
}
}
- Integer list contains the values. The Add method helps to insert values to the List.
- intlist.RemoveAt(5) remove the element from index position 5. As you know the index position is start from 0 and 6 is the value in index position-5. so when we run our application the output should like.
You can see the element 6 is not printed |
public class Program
{
static void Main(string[] args)
{
IList<int> intlist = new List<int>();
intlist.Add(10);
intlist.Add(20);
intlist.Add(30);
intlist.Add(40);
intlist.Add(50);
intlist.Add(60);
intlist.Add(70);
intlist.RemoveAt(5);
foreach (var num in intlist) {
Console.WriteLine(num + " ");
}
Console.ReadLine();
}
}
- Integer list contains the values. The Add method helps to insert values to the List.
- intlist.RemoveAt(5) remove the element from index position 5. As you know the index position is start from 0 and element 60 in index position-5 is to be remove. so when we run our application the output should like.
Difference between List and IList in C#
List | IList |
A class that represents a list of objects which can be accessed by index | IList is an interface that represents a collection of objects which can be accessed by index. |
List is a class | IList is an interface. |
List<T> has more helper methods | IList<T> has less helper methods |
List and IList are used to denote a set of objects. They can store objects of integers, strings, etc. There are methods to insert, remove elements, search and sort elements of a List or IList. The major difference between List and IList is that List is a concrete class and IList is an interface. Overall, List is a concrete type that implements the IList interface.
When to use List and when use IList in C#
- A List object allows you to create a list, add things to it, remove it, update it, index into it and etc. 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. Basically, if you want to create your own type of List, say a list class called ProductList, 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.
- IList is an Interface and cannot be instantiated. List is a class and can be instantiated. It means:
IList<string> MyList = new IList<string>();
List<string> MyList = new List<string>
Summary
Post a Comment