[C#6] 5-automatic Property enhancement

Source: Internet
Author: User
Tags mscorlib

0. Catalogue

C#6 new features Directory

1. Old Version Code
1 Internal class Person2 {3      Public stringName {Get;Private Set; }4      Public intAge {Get;Private Set; }5 6      PublicPerson (stringNameintAge )7     {8Name =name;9Age =Age ;Ten     } One}

Typically, the properties of C # can help us do the work well, like the code above. When assigning a value to a property, we can assign it anywhere. However, there is no syntax like a field declaration and an immediate initialization to simplify the setting of default values. C#6 brings us this new syntax, like assigning a value to a field as a value.

We also know that the properties of C # are actually a compiler-generated private field, get_xxx and set_xxx, and a piece of metadata, such as the code above compiled:

Il of the <name>k__backingfield field

1. fieldPrivate string '<name>k__backingfield'2. Custom instancevoid[mscorlib] System.runtime.compilerservices.compilergeneratedattribute::.ctor () = ( on xx xx xx ) 3. Custom instancevoid[mscorlib] System.diagnostics.debuggerbrowsableattribute::.ctor (valuetype [mscorlib] System.Diagnostics.DebuggerBrowsableState) = ( on xx xx xx xx xx xx xx)

Represents a private field, and the 2nd line indicates that this is automatically generated by the compiler, and that the 3rd line indicates that the field is not displayed in the debugger window.

Il of the Get_name method:

1. method PublicHidebysig SpecialName Instancestring 2 get_name () cil managed3 {4. Custom instancevoid[mscorlib] System.runtime.compilerservices.compilergeneratedattribute::.ctor () = ( on xx xx xx ) 5   //Code size 7 (0x7)6. maxstack87Il_0000:ldarg.08Il_0001:ldfldstringCsharp6. Person::'<name>k__backingfield'9 Il_0006:retTen}//end of Method Person::get_name

This is also an auto-generated method.

Il of the Set_name method:

1. methodPrivateHidebysig SpecialName Instancevoid 2Set_name (string 'value') CIL managed3 {4. Custom instancevoid[mscorlib] System.runtime.compilerservices.compilergeneratedattribute::.ctor () = ( on xx xx xx ) 5   //Code size 8 (0x8)6. maxstack87Il_0000:ldarg.08Il_0001:ldarg.19Il_0002:stfldstringCsharp6. Person::'<name>k__backingfield'Ten Il_0007:ret One}//end of Method Person::set_name

is also an auto-generated method.

The Il of the Name property:

1 string Name () 2 {3   .  Getstring  csharp6. Person::get_name ()4   .  Setvoid csharp6. Person::set_name (string)5//  End of property Person::name

Indicates that the Name property consists of a Get method and a set method.

2. Automatic attribute enhancement syntax
1 Internal class Person2 {3     //declaring read-write properties, and initializing default values4      Public stringName {Get;Set; } ="Blackheart";5 6     //declaring read-only properties, and initializing default values7      Public intAge {Get; } =1;8 9     //declaring read-only propertiesTen      Public stringNote {Get; } One  A      PublicPerson (stringNote) -     { -         //initialize default values for read-only properties in the constructor theNote =Note; -     } -  -     Private voidfunc1 () +     { -         //error, can only be initialized in the constructor +         //Note = "123"; A         //Age = 1; at         //can be modified because there is a set accessor -Name ="New name"; -     } -}

This new syntax sets the hidden private field to a read-only field (readonly) when there is no set accessor, allowing only the initial value to be set at the time of the declaration or to assign a value in the constructor. Look at IL:

Only the Name property has the Set_name method, and the age and note properties do not have a set accessor, and the corresponding private field is set to "initonly", which indicates that this is a read-only field.

constructor method, Name{get;set;} = "Blackheart" and Age{get;} The initialization operation portion of the =1 is transferred to the instance constructor ". ctor" method.

1. method Publichidebysig specialname rtspecialname2Instancevoid. ctor (stringNote) CIL managed3 {4   //Code Size (0x22)5. maxstack86Il_0000:ldarg.07Il_0001:ldstr"Blackheart"8Il_0006:stfldstringCsharp6. Person::'<name>k__backingfield'9Il_000b:ldarg.0TenIL_000c:ldc.i4.1 OneIL_000D:STFLD int32 csharp6. Person::'<age>k__backingfield' AIl_0012:ldarg.0 -Il_0013:call instancevoid[Mscorlib]system.object::.ctor () - Il_0018:nop the Il_0019:nop -Il_001a:ldarg.0 -Il_001b:ldarg.1 -Il_001c:stfldstringCsharp6. Person::'<note>k__backingfield' + Il_0021:ret -}//end of Method Person::.ctor

The code generated from the previous syntax can be said to be consistent, all of which are created as a field, Get_xxx, and Set_xxx methods and corresponding attribute metadata, which remains essentially the compiler's syntax simplification.

3. Reference

C # Auto-property Enhancements

[C#6] 5-automatic Property enhancement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.