In this article we will discuss about when to use IEnumerable, ICollection and IList with example. IEnumerable, ICollection and IList are important interfaces in C# for working with collection. IEnumerable is the base of the ICollection and IList interfaces
IEnumerable
- IEnumerable is an interface in C# representing a collection of elements that can be enumerated. The IEnumerable interface is defined in the System.Collections namespace.
- The interface defines a single method, GetEnumerator(), which returns an IEnumerator object that allows you to iterate over the collection
- IEnumerable supports lazy evaluation, which means that the collection elements are not computed until they are actually needed. This can improve performance by avoiding unnecessary computations.
}} // create list for fruits
List<string> fruits = new List<string>();
fruits.Add("Apple");
fruits.Add("Banana");
fruits.Add("Orange");
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
IEnumerator enumerator = fruits.GetEnumerator();
while (enumerator.MoveNext())
{
string fruit = (string)enumerator.Current;
Console.WriteLine(fruit);
}
IEnumerable<string> query = fruits.Where(fruit => fruit.Length > 5);
foreach (string fruit in query)
{
Console.WriteLine(fruit);
}
}
In this example, we create a List<string> object called fruits that contains three different fruits
- We use a foreach () loop to iterate over the fruits list and print each element to the console
- We create an IEnumerator object by calling the GetEnumerator() method on the fruits list and then use a while loop to iterate over the elements of the collection and print each one to the console
Then use a foreach loop to iterate the elements and print each one to the console.
ICollection
- ICollection is an interface in the .NET Framework that Represent a non-generic collection of objects.
- The difference between ICollection and IEnumerable is that ICollection is a write operation and IEnumerable is a read-only operation only meant for iteration.
- When can I use ICollection?
If we want some functionality like,
- Count( )
- Add( )
- Clear( )
- Remove( )
- IsReadOnly( )
- Contains( )
Source code:
internal class Array
{
private static void Main(string[] args)
{
var monthsofYear = new List<string>
{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December" };
//casted to ICollection
ICollection<string> collection = (ICollection<string>) monthsofYear;
Console.WriteLine(collection.Count);
Console.ReadLine();
}
}
}
In this example we create a collection<string>. That contains months of year.
The list “monthsOfYear” is casted to an interface type “ICollection<string>”. The purpose of this casting is to treat the list as a collection and access the properties and methods provided by the ICollection interface.
The code prints the numbers of elements in the collection using Count() property of the ICollection<string>.
Finally, the program waits for the user to press the enter key before exiting, using the Console.ReadLine() method.
IList
- IList is an Interface that represents a non-generic collection of objects that can be accessed by index.
- IList inherits from the ICollection interface, which means it inherits the basic operation defined in ICollection and adds additional Operations.
- IList provides us full flexibility to expose any method that list implements.
- IList defines some properties and methods
- Count()
- ReadOnly()
- Add()
- Insert()
- Remove()
- RemoveAt()
- Contains()
- IndexOf()
Source code:
public class Person
{
public string name { get; set; }
public string surname { get; set; }
}
class Program
{
static void Main(string[] args)
{
//Implement IList into List
Person p1 = new Person();
p1.name = "Sourav";
p1.surname = "Kayal";
IList list = new List<Person>();
list.Add(p1)
Console.WriteLine(((Person)list[0]).name + " " + ((Person)list[0]).surname);
Console.ReadLine();
}
}
}
This class have two properties: name and surname. An Empty List<person> is created.
The p1 object is added to the list using Add method provided by the IList interface. This adds the person object to the end of the list.
The Console.WriteLine method to print the value of the name and surname properties of the first element in the list
Conclusion In this example we learned the concept of IEnumerable, ICollection, IList. I hope you got a clear understanding about IEnumerable, ICollection, IList.
No Comment! Be the first one.