Why is there a difference in behaviour of an application built in VS 2010 v.s. VS 2012?

前端 未结 2 1792
梦如初夏
梦如初夏 2021-02-07 04:08

I was checking whether installing .NET 4.5 on our build machines changes the output IL image generated by VS 2010.

Since I know the behaviour of foreach has changed in .

2条回答
  •  鱼传尺愫
    2021-02-07 04:49

    Visual Studio uses an in-process compiler, so it knows which version of C# it's using.

    As you noted, csc.exe from the command line, on the other hand, uses whatever C# version it's made to compile, so in your case it'll be C# 5.0. Since it's an in-place upgrade (in terms of installation directory), it might break code that relied on the foreach binding being the same across the whole loop (odd, but possible).


    NOTE: Old answer for the wrong question: the OP knows this and was testing it from the command line.

    The blog post you link to already answers your question. I think this question is related to this one.

    It's the compiler that changed, so this:

    foreach (int value in values)
    {
        // ...
    }
    

    used to generate something along the following code:

    {
        int value;
        for (/* iteration */)
        {
            value = /* get from enumerator */;
            // ...
        }
    }
    

    while the new C# compiler now generates the equivalent of moving the variable to inside the loop:

    for (/* iteration */)
    {
        int value = /* get from enumerator */;
        // ...
    }
    

    This makes a great difference, since closures within the // ... will capture a new value binding in each cycle, instead of sharing the same value binding that used to be declared outside the loop.

    The catch is, if you want your code to work correctly for both older and newer compilers, you must declare your own variable inside the foreach loop:

    foreach (int value in values)
    {
        int newValue = value;
        // ...
    }
    

    The current C# 4.0 specification in Visual Studio 2010 says:

    (...) A foreach statement of the form

    foreach (V v in x) embedded-statement
    

    is then expanded to:

    {
      E e = ((C)(x)).GetEnumerator();
      try {
          V v;
          while (e.MoveNext()) {
              v = (V)(T)e.Current;
              embedded-statement
          }
      }
      finally {
          … // Dispose e
      }
    }
    

    The C# 5.0 specification in Visual Studio 2012 says:

    (...) A foreach statement of the form

    foreach (V v in x) embedded-statement
    

    is then expanded to:

    {
      E e = ((C)(x)).GetEnumerator();
      try {
          while (e.MoveNext()) {
              V v = (V)(T)e.Current;
              embedded-statement
          }
      }
      finally {
          … // Dispose e
      }
    }
    

提交回复
热议问题