In C #3.0, defining attributes is more convenient. You do not need to be as complicated as in the previous version. You need to first define the fields for storing data, and then define the attribute server, now, you only need to define the attribute server. Other compilers automatically complete the task for us to save the time required to define fields; during object initialization, We can initialize object properties during object construction, similar to set initialization.
1. Anonymous attributes
The attributes are defined as follows:
public class Employee { public int Id { get; set; } public string Name { get; set; } public string Sex { get; set; } public int Age { get; set; } public string BirthDate { get; set; } }
The statement before C #3.0 is as follows:
public class Employee { private int _id; private string _name; private string _sex; private int _age; private string _birthDate; public int Id { get { return _id; } set { _id = value; } } public string Name { get { return _name; } set { _name = value; } } public string Sex { get { return _sex; } set { _sex = value; } } public int Age { get { return _age; } set { _age = value; } } public string BirthDate { get { return _birthDate; } set { _birthDate = value; } } }
The former reduces the code size by 2/3 compared with the latter, which is obvious in improving efficiency. Where are these codes? In fact, the extra code is done by the compiler for us. The compiler will compile the code that we "save" into the managed il code and fill it in, the size of the final il code generated by the two codes is similar.
In the Il code, we can see that fields such as <XXX> k_backingfield are automatically generated by the compiler.
2. object initializer, the original object initialization must first create a constructor before member operations can be performed. C #3.0 provides the ability to directly initialize object members, initialize an object like initializing a set or array.
In the aspect, the above employee class is called through the object initializer:
Employee Employee = new employee {id = 1, name = "blue wind", age = 24, birthdate = "1984-10-21", sex = "male"}; console. writeline ("No.; {0}", employee. ID); console. writeline ("Name: {0}", employee. name); console. writeline ("Age: {0}", employee. age); console. writeline ("birthday: {0}", employee. birthdate); console. writeline ("Gender: {0}", employee. sex); console. writeline ("press any key to continue... "); console. readline ();
This sentence
Employee Employee = new employee {id = 1, name = "blue wind", age = 24, birthdate = "1984-10-21", sex = "male "};
It is the initialization of the object. The code is concise and the output result is as follows:
The practice before C #3.0 is:
Employee Employee = new employee (); employee. id = 1; employee. name = "blue wind"; employee. age = 24; employee. birthdate = "1984-10-21"; employee. sex = "male"; console. writeline ("No.; {0}", employee. ID); console. writeline ("Name: {0}", employee. name); console. writeline ("Age: {0}", employee. age); console. writeline ("birthday: {0}", employee. birthdate); console. writeline ("Gender: {0}", employee. sex); console. writeline ("press any key to continue... "); console. readline ();
Or you can initialize these attributes by reloading the constructor. The effects of the two attributes are the same, but the former is easier to use and reduces the amount of code, how is this process completed? In fact, C # itself has not changed much. These are all syntactic changes, making it easier and more efficient to write code, let the compiler do some work that can be inferred from the compiler. when compiling the program, the compiler will implement the code we didn't implement for us to generate the Il code:
Method private hidebysig static void main (string [] ARGs) cel managed {. entrypoint // code size: 175 (0xaf ). maxstack 2. locals Init ([0] class cs30.employee employee, [1] class cs30.employee '<> G _ initlocal0') il_0000: NOP il_0001: newobj instance void cs30.employee ::. ctor () il_0006: stloc.1 il_0007: ldloc.1 il_0008: LDC. i4.1 il_0009: callvirt instance void cs30.employee: set_id (int32) il_000e: NOP il_00 0f: ldloc.1 il_0010: ldstr bytearray (DD 84 4B 4E ce 98 )//.. kn .. il_0015: callvirt instance void cs30.employee: set_name (string) il_001a: NOP il_001b: ldloc.1 il_001c: LDC. i4.s 24 Response: callvirt instance void response: set_age (int32) il_0023: NOP il_0024: ldloc.1 il_0025: ldstr "1984-10-21" il_002a: callvirt instance void response: callback (string) response: NOP il_0030: ldloc. 1 il_0031: ldstr bytearray (37 75) // 7u il_0036: callvirt instance void metadata: set_sex (string) il_003b: NOP il_003c: ldloc.1 il_003d: stloc.0 il_003e: ldstr bytearray (16 7f F7 53 3B 00 7b 00 30 00 7d 00 )//... s ;. {. 0 .}. il_0043: ldloc.0 il_0044: callvirt instance int32 cs30.employee: get_id () il_0049: Box [mscorlib] system. int32 il_004e: Call void [mscorlib] system. console: writeline (Str ING, object) il_0053: NOP il_0054: ldstr bytearray (D3 59 0d 54 3A 00 7b 00 30 00 7d 00 )//. y. T :. {. 0 .}. il_0059: ldloc.0 il_005a: callvirt instance string cs30.employee: get_name () il_005f: Call void [mscorlib] system. console: writeline (string, object) il_0064: NOP il_0065: ldstr bytearray (74 5E 84 9f 3A 00 7b 00 30 00 7d 00) // t ^ .. :. {. 0 .}. il_006a: ldloc.0 il_006b: callvirt instance int32 CS3 0. employee: get_age () il_0070: Box [mscorlib] system. int32 il_0075: Call void [mscorlib] system. console: writeline (string, object) il_007a: NOP il_007b: ldstr bytearray (1f 75 E5 65 3A 00 7b 00 30 00 7d 00 )//. u. E :. {. 0 .}. il_0080: ldloc.0 il_0081: callvirt instance string cs30.employee: get_birthdate () il_0086: Call void [mscorlib] system. console: writeline (string, object) il_008b: NOP il_008c: lDs Tr bytearray (27 60 2B 52 3A 00 7b 00 30 00 7d 00) // ''+ R :. {. 0 .}. il_0091: ldloc.0 il_0092: callvirt instance string cs30.employee: get_sex () il_0097: Call void [mscorlib] system. console: writeline (string, object) il_009c: NOP il_009d: ldstr bytearray (F7 8B 09 63 FB 4E 0f 61 2E 95 E7 7E ed 7E 2E 00 //... c. n. a... ~. ~.. 2e 00 2E 00 )//.... il_00a2: Call void [mscorlib] system. console: writeline (string) il_00a7: NOP il_00a8: Call string [mscorlib] system. console: Readline () il_00ad: Pop il_00ae: Ret} // end of method program: Main is clearly displayed from the above il code. First, the employee instance is created, and then assign values to relevant attributes.
3. Conclusion:
Both the automatic attributes and the object initializer are improved syntax-level functions provided by C #3.0. It is a syntactic sugar that makes code writing more efficient and delivers repetitive work to the compiler, however, this change also increases the opacity of the Code, which is more prominent in the implicit type and increases the difficulty of understanding the code, these are just a choice for code writers. If you do not like it, you can write your own code in the original way.