Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

271
Vistas
What does "object is enumerated" mean in C#?

I've been reading lately articles and documentation about deferred execution, LINQ, querying in general etc. and the phrase "object is enumerated" came up quite often. Can someone explain what happens when an object gets enumerated?

Example article.

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C#

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

General explainations for enumeration

IEnumerable is an interface, that is usually implemented by collection types in C#. For example List, Queue or Array.

IEnumerable provides a method GetEnumerator which returns an object of type IEnumerator.

The IEnumerator basically represents a "forward moving pointer" to elements in a collection. IEnumerator has:

  • the property Current, which returns the object it currently points to (e.g. first object in your collection).
  • a method MoveNext, which moves the pointer to the next element. After calling it, Current will hold a reference to the second object in your collection. MoveNext will return false if there were no more elements in the collection.

Whenever a foreach loop is executed, the IEnumerator is retreived and MoveNext is called for each iteration. The variable you define in your loop header, is filled with the IEnumerator's Current.


Compiling a foreach loop

thx to @Llama

This code...

List<int> a = new List<int>();
foreach (var val in a)
{
    var b = 1 + val;
}

is transformed to something like this by the compiler:

List<int> list = new List<int>();
List<int>.Enumerator enumerator = list.GetEnumerator();
try
{
    while (enumerator.MoveNext())
    {
        int current = enumerator.Current;
        int num = 1 + current;
    }
} finally {
    ((IDisposable)enumerator).Dispose();
}

There is no guarantee that a foreach results in GetEnumerator actually being called - however it's a quite common thing to do for the compiler.


The quote

The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C#.

Basically it tells you everything already. I think the confusion only comes from the lack of knowledge about enumeration. As soon as someone calls GetEnumerator, the method is executed. GetEnumerator is automatically called, as soon as you put your object into a foreach loop.

over 4 years ago · Santiago Trujillo Denunciar

0

I think you just need a good understanding of deferred vs. immediate execution for LINQ queries.

Deferred execution: When you write a LINQ query it is only executed when you actually access the results - deferred until you run code to iterate with i.e. a foreach over the results. This is the default behaviour.

Immediate execution: We can force this (which you'll see often in C# code) by appending a ToList() or similar method to the query.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda