IEnumerable<T>
Interface
Exposes the enumerator, which supports a simple iteration over a collection of a specific type.
Interface declaration
public interface IEnumerable<out T> : IEnumerable
Type parameters
The generic type parameter T is used to specify the type of elements in the collection.
Remarks
The IEnumerable<T>
interface is the base interface for all generic collection types in the .NET Framework.
It provides a standard way to iterate over the elements of a collection. When you implement IEnumerable<T>
, you must implement the GetEnumerator
method, which returns an enumerator for the collection.
The out
variance modifier on the type parameter T
allows you to use a more general type than what is specified by the generic type parameter. For example, you can assign a List<string>
object to a variable of type IEnumerable<object>
.
Members
-
GetEnumeratorIEnumerator<T> GetEnumerator()Returns an enumerator that iterates through the collection.
-
System.Collections.IEnumerable.GetEnumeratorIEnumerator IEnumerable.GetEnumerator()Returns an enumerator that iterates through a collection.
Implements
Derived by
Many types in the .NET Framework, including:
- List<T>
- Dictionary<TKey, TValue>
- Array
- And many more...
Examples
Iterating with foreach
var numbers = new List<int> { 1, 2, 3 };
foreach (var number in numbers)
{
Console.WriteLine(number);
}