Why is Action.Method.IsStatic different between Visual Studio 2013 and 2015 for specific lambda expressions

Given the console program below:

class Program { private static string _value; static void Main(string[] args) { var t = new Action(() => _value = "foo"); Console.Out.WriteLine("t.Method.IsStatic: {0}", t.Method.IsStatic); } } 

When compiling with .Net 4.5.2 using VS 2013, it will print

 t.Method.IsStatic: true 

When compiling with .Net 4.5.2 using VS 2015, it will print

 t.Method.IsStatic: false 

From this question, I seem to understand what is happening, but I am confused why there is a change in behavior between versions of VS. In my opinion, the 2013 exit is correct.

+5
source share
2 answers

Check the answer at the following link: Delegate changes in caching behavior in Roslyn

Basically what has changed, and I quote @Yuzal from a related answer:

"In Roslyn, the delegate caching behavior was changed. Previously, like any lambda expression that did not commit variables, it was compiled into a static method on the call site. Roslin changed this behavior. Now any lambda that captures variables or not is converted to a class display: "

And according to the display class, he had in mind the generated closed private class, inside which the instance method, called by the action delegate, is encapsulated.

Why was the change made? Quoting @Kevin Pilch-Bisson (member of C # ID team):

The reason for this is faster, because delegate calls are optimized for instance methods and have room for a stack for them. To call the static method, they must shift parameters around.

So basically the commentary itself explains. The behavior you see in the above example is that they noticed that if an Action delegate is invoked by instance methods, it is faster than calling static methods, regardless of whether the lambda captures variables or not.

+4
source

From it was shown that the behavior has changed between the 2013 compiler and Rosyln. Very annoying.

0
source

All Articles