In this blog we can see what is IEnumerable in C# .Net.
What is IEnumerable in C#?
In C#, the interface IEnumerable defines the method GetEnumerator, which produces an IEnumerator interface as a result. A collection that implements IEnumerable can therefore be used with a for-each statement if this enables read-only access to it.
Key Points :
- The System.Collections are contained in the IEnumerable interface.Common namespace.
- A generic interface called IEnumerable enables looping over generic and non-generic lists.
- Linq query expressions can also be used using the IEnumerable interface.
- Interface IEnumerable gives back an iterator that goes through the collection of items.
Example:
public class Customer : IEnumerable {
public IEnumerator GetEnumerator() {
throw new NotImplementedException();
}
}
What is IEnumerator in C# .Net?
An interface called IEnumerator makes it easier to retrieve the collection’s most recent elements.
IEnumerator Contain 2 methods
- MoveNext()
- Reset()
MoveNext() :
Sets the enumerator to the collection’s next element; it returns true if the operation was successful and false if the enumerator has reached the collection’s end.
Reset() :
returns the enumerator to its starting place, which is in front of the collection’s first element.
Example :
public class Customer : IEnumerator
{
public object Current
{
get { throw new NotImplementedException(); }
}
public bool MoveNext()
{
throw new NotImplementedException();
}
public void Reset()
{
throw new NotImplementedException();
}
}
For any Help or Queries Contact us on info@crmonce.com or +918096556344