In this article, we will discuss the difference between the List vs IEnumerable in C# with examples. We will start with a brief introduction to data structures, coding implementations and then proceed to the comparison to each of them. Their usage and useful practical coding examples will help us to decide when and what should be used in our code.
The main difference between Dictionary and Hashtable is that IEnumerable and the List are also part of .NET's System.Collections namespace. The fundamental distinction between List and IEnumerable in C# is that List is a concrete class, whereas IEnumerable is an interface. Moreover, IEnumerable is read-only and List is not. IEnumerable provides an interface for getting the next item one by one (enumerating data), whereas the List represents the entire collection in memory.
List in C# :
Strongly typed objects can be found in the List<T> collection, which also has methods for sorting, searching, and list modification. It belongs to the System.Collections.Generic namespace and is an ArrayList in generic form. The highly typed list of objects represented by this generic list collection class can be accessed by using an integer index that begins at 0. It also offers a variety of tools for filtering, organizing, and searching the list of things.
Using the Generic List Collection Class in C#, we can create a collection of any data type. For instance, if you wish to, you can make lists of strings, integers, and doubles. You can even create lists of user-defined complicated kinds, such as lists of customers, items, students, and so on.
The Add() and AddRange() methods need to be used if you wish to add items to the generic list collection. You'll see that the Add and Add functions need data of type T. And in this case, T merely refers to the type of argument you must supply when making an instance of the Generic List Class.
Methods of List:
Add(T item): To add an element to the Generic List's end, use the Add(T item) method. The object that will be added to the end of the generic list is specified in this case by the parameter item. When using a reference type, the value may be null.
AddRange(IEnumerable<T>collection): The AddRange(IEnumerable<T> collection) method is used to add the Elements of the specified collection to the end of the Generic List. The collection whose members are to be tacked onto the end of the generic list is specified by the argument collection. Even while the collection itself cannot include null elements, it can if type T is a reference type.
As you can see in the code below, we are setting the type argument as a string when constructing the Generic List collection instance, allowing the collection to only store entries of the string type. In this instance, the element will be added to the collection at the end.
List<string>
countries = new List<string>();
countries.Add(“India”);
countries.Add(“Srilanka”);
Example:
// Dynamic ArrayList with no size limit
List<int> numberList = new List<int>();
numberList.Add(32);
numberList.Add(21);
numberList.Add(45);
numberList.Add(11);
numberList.Add(89);
// List of string
List<string> authors = new List<string>(5);
authors.Add("India");
authors.Add("USA");
authors.Add("U.K");
authors.Add("AUSTRALIA");
authors.Add("PAKISTAN");
authors.Add("SOUTH AFRICA");
Adding Elements using the AddRange Method of the List Class.
The following is our second collection.
List<string> newCountries = new List<string>();
newCountries.Add(“USA”);
newCountries.Add(“UK”);
Now, we want to add our second collection at the end of the first collection. To do so, we need to use the AddRange method as follows:
countries.AddRange(newCountries);
Example:
// Collection of string
string[] country = { "india", "australia", "england" };
// Create a List and add a collection
List<string> countries = new List<string>();
itList.AddRange(country);
foreach (string a in countries)
Console.WriteLine(a);
IEnumerable in C# :
In C#, the interface IEnumerable defines the method GetEnumerator, which produces an IEnumerator interface as a result. A collection that implements IEnumerable can therefore be used with a for-each statement if this enables read-only access to it. IEnumerable is an interface that is available in System.Collection namespace.
IEnumerable is an interface that simply defines the GetEnumerator function. An IEnumerator interface is the result of the method. This interface is used to iterate collections of anonymous type classes or lists. It also functions with LINQ query expressions. All non-generic lists have the IEnumerable interface as their base interface. The IEnumerable interfaces have four extension methods. These are the functions AsParallel(), AsQueryable(), CastTResult>(), and OfTypeTResult> ().
Extension Methods in IEnumerable C#:
Given below are the extension methods in c#:
Cast<TResult>(IEnumerable): The non-generic collection of the IEnumerable interface is converted to the specified type mentioned.
OfType<TResult>(IEnumerable): The elements of the IEnumerable are filtered based on the type mentioned.
AsParallel(IEnumerable): This is used to enable the running of parallel queries.
AsQueryable(IEnumerable): This is used to convert the IEnumerable interface to the IQueryable interface.
Syntax:
IEnumerable<TSource>
Let us implement the IEnumerable interface in a class as:
public class Customer : IEnumerable
{
public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}
You can see from the example above that, after implementing the IEnumerable Interface, there is a method called GetEnumerator and an interface called IEnumerator that can be used to retrieve the collection's most recent members.
Most C# collections and all
C# arrays implement IEnumerable<T>
. Here are
some examples:
IEnumerable<int> list = new List<int> { 1, 2, 3 };
IEnumerable<int> array = new[] { 1, 2, 3 };
IEnumerable<int> set = new SortedSet<int> { 1, 2, 3 };
Difference between List vs IEnumerable in C#:
List |
IEnumerable |
List is included in the System.Collections namespace.
|
IEnumerable is included in the System.Collections namespace. |
The list is not read-only, enumeration can be mutated so can add and remove records.
|
IEnumerable is read-only, it provides an interface for getting the next item one by one (enumerating data).
|
The list is a concrete class.
|
IEnumerable is an interface. |
The List is more efficient when you need to enumerate the data multiple times because it already has all of it in memory.
|
IEnumerable is more efficient and faster when you only need to enumerate the data once.
|
The list will execute the query as soon as it’s called.
|
IEnumerable will not execute the query until you enumerate the data.
|
The list uses IList, a non-generic interface, internally. IList allows for more direct manipulation of the contents of a list and is the recommended way to manipulate a list.
|
IEnumerable uses IEnumerator internally and is the recommended way to enumerate the contents of a collection.
|
Executed immediately and creates a new list with the contents of the enumeration in memory.
|
Deferred execution, meaning that a query will not get executed until iterating. |
Conclusion:
Strongly typed objects can be found in the List<T> collection, which also has methods for sorting, searching, and list modification. In the Generic List Collection Class in C#, we can create a collection of any data type. For instance, if you wish to, you can make lists of strings, integers, and doubles.
In C#, the interface IEnumerable defines the method GetEnumerator, which produces an IEnumerator interface as a result. A collection that implements IEnumerable can therefore be used with a for-each statement if this enables read-only access to it.
hope you enjoyed reading this article and found it useful. Please share your thoughts and recommendations in the comment section below.
Share This Post
Support Me