Today is nothing, and then a vs2015 preview, the previous period of time the garden is also in stir these new grammar sugar, here we come to see what kind of il will be generated?
One: Automatic initialization of properties
It's a bit simpler than the previous version, but you must be curious as to what the compiler has done for us.
1 class Student {"ctrip"; 4}
As you can see from this diagram, the assignment of <name>k__backingfield= "Ctrip" in ctor before Base::ctor, which means that Name is the variable initialization assignment, not the
constructor assignment, what is the difference, if Base::ctor before <name>k__backingfield= "Ctrip", that is the constructor assignment, but I have to specifically indicate
, it is the difference between the source code level, not the difference in IL, because the constructor is assigned in IL, but the order of the statements is different, then I restore the internal code as follows:
1ClassStudent2{3PrivateString K__backingfield ="Ctrip";45PublicString Name 6 { 7 get 8 { 9 return K__backingfield;}11 12 set13 {14 k__ Backingfield = Value; }16 }17}
And then see how to get Base::ctor before <name>k__backingfield= "Ctrip".
1ClassStudent2{3PrivateStringK__backingfield;45PublicStringName6{7Get8{9ReturnK__backingfield;10}One set of {K__backingfield = value; () (): Student () k__backingfield = "Ctrip";
21
}
I'm sorry, I just got it. Variable initialization and constructor assignment are distinguished at the source code level.
Second: read-only property initialization
This is also a super fun attribute, first look at the code:
1 class student< Span style= "color: #008080;" >2 {3 public string Name {get;} 4 5 public Student ( Span style= "color: #0000ff;" >string name" 6 {7 Name = Name; 8 }9}
But we remember that in the previous C # version it was not possible to write this, but now we can't get curious, first go to the bottom to see what is generated.
Then I was surprised, the property can be read-only, and now the compiler has let go, that is not a problem, if I really need a read-only property, this how to be good
It? Then I tried to return a value in the Name property, and the compiler did not release it, which means the compiler did a seemingly reasonable judgment inside.
Three: lambda acts as a function body
This may sound a bit strange, but let's look at an example.
1 class Student {"ctrip"; void Print (string name) = Console.WriteLine (name); 6}
But when I see this writing, I am also drunk, if you do not contact C # for a year, and then come back to see, I think you can not understand these dick code ... There's no way to go.
See what IL has done on the ground floor?
When I see Il again drunk, in fact, and is only a {} Method body bracket! Is this not an increase in our learning costs? And then I went on to think that the body of the function here is a
Console.wirteline statement, what if I fill in a few sentences? You can imagine it must be parentheses, but I really add {}, the compiler is messy ...
Then this figure tells us that c#6.0 's Lamaba serves as a syntax for a function body, and if you really want to do multiple statements, you can only extract a single method,
Just like this.
Well, the last article in general on this, the time is not early, first withdrew.
See what those grammar candies in C # 6.0 do (the previous article)