The IEnumerable interface provides a generic way to iterate through a collection. It exposes a single method, GetEnumerator(), which returns an enumerator that can be used to loop through the elements in the collection.
Example
IEnumerable<string> colors = new List<string> { "Red", "Green", "Blue" };
foreach (string color in colors)
{
Console.WriteLine(color);
}
IQueryable
The IQueryable interface is used for querying data from a data source, typically a database, by defining a query using expressions and providing an IQueryProvider. This allows for deferred execution of queries.
The ICollection interface extends IEnumerable and adds methods for managing a collection, such as Count, Contains, Add, and Remove.
Example
ICollection<string> colors = new List<string> { "Red", "Green", "Blue" };
int count = colors.Count;
bool containsGreen = colors.Contains("Green");
colors.Add("Yellow");
colors.Remove("Red");
IList
The IList interface extends ICollection and provides additional methods for working with a list, including IndexOf, Insert, and RemoveAt.
Example
IList<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
int cherryIndex = fruits.IndexOf("Cherry");
fruits.Insert(1, "Orange");
fruits.RemoveAt(0);
List
List is a concrete class in C# that implements the IList interface. It provides a dynamic array that can be used to store and manipulate elements efficiently.
Example
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
numbers.Add(6);
numbers.Remove(2);
int thirdNumber = numbers[2];