Are previous versions of the C # compiler just emulation in Visual Studio?

C # 5 introduced an interrupt change to how loop loop closures work in the foreach , which is well known (for details, see Eric Lippert's article). I wanted to see a pre-tasking behavior change in Visual Studio. To do this, I created a small console application with this code:

 using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var values = new List<int>() { 100, 110, 120 }; var funcs = new List<Func<int>>(); foreach (var v in values) funcs.Add(() => v); foreach (var f in funcs) Console.WriteLine(f()); } //this method does break compilation on C# versions that are older than C#5 public async void DoStuff() { } } } 

When compiling with a C # 5 compiler, it explicitly prints

 110 120 130 

But after I went to Project|Properties|Build|Advanced , changed the compiler to C # 3 and commented on the DoStuff method, it still printed the same numbers, not

 130 130 130 

as i expected.

After that, I examined the IL codes for the Main methods created by both compilers, and they were completely identical.

So my question is: are there any language versions of C # that I can choose in Visual Studio, in fact the entire latest version of the compiler with some language restrictions, and if so, what is the reason for this solution?

+6
source share

Source: https://habr.com/ru/post/987448/


All Articles