Question

I have a nice question about the loop-expression of a for-loop in C#.

I always like to learn and extend my knowledge so I often use my reflector to look and learn from other. Lately I was looking around in an assembly which I assume is a C# assembly.

I came across this:

public virtual void DoRows(Parse rows)
{
  for (; rows != null; {
    Parse more;
    rows = more;
  }
  )
  {
    more = rows.More;
    this.DoRow(rows);
  }
}

I never knew I could use a code-block in my loop-expression. So I fired up MSDN and went looking in the language reference of C# but found nothing.

The C++ reference shows some interesting things tho (http://msdn.microsoft.com/en-us/library/b80153d8.aspx), but I am not a C++ developper and I have a strong feeling the assmbly was not written in C++.

dotPeek says the assembly is a v4.0.30319 / msil assembly.

Now here are the questions: - Is this a C++ or a C# code-construct?! - Can we do things like this in C#?

I am not able to reproduce code that looks like that and compiles in VS2010.

@edit: changed the word assembly in code-construct.

@edit II: Thanks for all your answers. I think this is either a bug in the dotPeek reflector or we found some for-loop-expression easter eggs. This is what Red Gate says about the same method:

public virtual void DoRows(Parse rows)
{
    while (rows != null)
    {
        Parse more = rows.More;
        this.DoRow(rows);
        rows = more;
    }
}
Was it helpful?

Solution

Compilation to IL is a many-to-one operation and decompilers have rules regarding which of the many choices they choose when decompiling. Often, different languages produce IL that is not possible to reproduce directly in another language.

In this instance either the decompiler has chosen poorly and the produced C# is invalid (a bug in the decompiler), or it cannot decompile the code into equivalent C#.

OTHER TIPS

There is no such thing as a C++ or C# assembly. Once the code is compiled, it is ILCode. The reflector takes this ILCode and decompiles it to the language you selected.

This can result in code that is valid in ILCode, but not valid in the target language.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top