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

}}            // 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

Then use a foreach loop to iterate the elements and print each one to the console.

ICollection

   If we want some functionality like,

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    

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.

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.