C# IEnumerable
In C#, IEnumerable is an interface that is used to iterate over a collection of objects. The interface defines a single method called GetEnumerator() that returns an IEnumerator object, which can be used to iterate over the collection. In this tutorial, we'll discuss how to use IEnumerable in C#.
Syntax
The syntax for defining an IEnumerable in C# is as follows:
interface IEnumerable {
IEnumerator GetEnumerator();
}
The IEnumerable interface defines a single method called GetEnumerator() that returns an object that implements the IEnumerator interface.
Example
Let's create a simple example to demonstrate how to use IEnumerable in C#. We'll create a custom collection class called "PersonCollection" that implements the IEnumerable interface. Here's how we can implement it:
class PersonCollection : IEnumerable {
private List<Person> people = new List<Person>();
public void AddPerson(Person p) {
people.Add(p);
}
public IEnumerator GetEnumerator() {
return people.GetEnumerator();
}
}
We'll also define a class called "Person" to represent a person:
class Person {
public string Name { get; set; }
public int Age { get; set; }
}
Now, we can use our custom collection class to iterate over the list of people we've added:
var people = new PersonCollection();
people.AddPerson(new Person { Name = "John", Age = 30 });
people.AddPerson(new Person { Name = "Jane", Age = 25 });
foreach (Person p in people) {
Console.WriteLine(p.Name + " - " + p.Age);
}
Output
When we run the example code above, the output will be:
John - 30
Jane - 25
This is because we used the foreach loop to iterate over each person in the collection and print their name and age to the console.
Explanation
In the example above, we created a custom collection class called "PersonCollection" that implements the IEnumerable interface. We also defined a Person class to represent a person.
We added some people to our custom collection class and then used the foreach loop to iterate over each person in the collection and print their name and age to the console.
The foreach loop is a convenience feature of C# that allows us to easily iterate over collections without having to manually manage the IEnumerator object.
Use
IEnumerable is commonly used in C# to provide iteration over custom collection classes. It enables you to create your own collection classes and allows them to be iterated over using a foreach loop. IEnumerable provides more flexibility than arrays and can be used with any collection class, not just with arrays.
Important Points
- In order to use IEnumerable, you must implement the GetEnumerator() method and return an object that implements the IEnumerator interface.
- The foreach loop in C# is a convenient way to iterate over a collection of objects and automatically manage the IEnumerator object for you.
- IEnumerable is commonly used in C# to provide iteration over custom collection classes.
Summary
In this tutorial, we discussed how to use IEnumerable in C#. We covered the syntax, example, output, explanation, use, and important points of IEnumerable in C#. With this knowledge, you can now use IEnumerable to iterate over collections of objects and create your own collection classes.