The main difference between List and IList in C#.NET is that List is a class that represents a list of objects which can be accessed by index while IList is an interface that represents a collection of objects which can be accessed by index.
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.
Key Areas Covered
1. What is List in C#.NET
– Definition, Functionality
2. What is IList in C#.NET
– Definition, Functionality
3. Difference Between List and IList in C#.NET
– Comparison of Key Differences
Key Terms
C#.NET, Class, Interface, IList, List
What is List C#.NET
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>();
The above List object can store a list of integers.
In the above program, obj is an object of type List. The Add method helps to insert values to the List. Line 15 indicates to eliminate the element at index 2. Therefore, element 3 is eliminated from the list. Finally, the “for each” loop prints all the elements available in the List.
What is IList C#.NET
It is possible to create a IList object as follows.
IList<int> intList = new List>int>();
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>.
In the above program, obj is an object of type IList. The Add method helps to insert values to the IList. Line 15 indicates to eliminate the element at index 2. Therefore, the element 30 is eliminated from the list. Finally, the “for each” loop prints all the elements available in the List.
Difference Between List and IList in C#.NET
Definition
List is a class that represents the list of objects which can be accessed by the index while IList is an interface that represents a collection of objects that can be individually accessed by the index.
Basis
Thus, the main difference between List and IList in C#.NET is that while List is a class, IList is an interface.
Number of helper methods
Moreover, List<T> has more helper methods than IList<T>. Hence, this is another difference between List and IList in C#.NET.
Conclusion
There is a difference between List and IList even though some people use these terms interchangeably. The main difference between List and IList in C#.NET is that List is a class that represents a list of objects which can be accessed by index while IList is an interface that represents a collection of objects which can be accessed by index.
References:
1.“C# List.” C# List, Available here.
2.Mairaw. “IList Interface (System.Collections.Generic).” IList Interface (System.Collections.Generic) | Microsoft Docs, Available here.
3.“C# | List Class.” GeeksforGeeks, 3 Apr. 2019, Available here.
Leave a Reply