Object initializer
First declare a class person:
Public ClassPerson {Public StringName {Get;Set;}Public IntAge {Get;Set;}}
Initialize it and call it:
Static VoidMain () {person=NewPerson {name ="Dancing", Age =22}; Console. writeline ("Name: {0}", Person. Name); console. writeline ("Age: {0}", Person. Age. tostring ());}
This is a trick of the compiler: ilCodeIt shows that it works exactly the same as the general initialization operation. {} Must contain a common member, field, or attribute. You can support object initiators. To satisfy one condition, you must have a public constructor without parameters.
Set Initiator
Static Void Main () {list <Person> personlist = New List <person> { New Person {name = " Dance " , Age = 22 }, New Person {name = " Spring and Autumn " , Age = 21 }}; For ( Int I = 0 ; I <personlist. Count; I ++ ) {Console. Write (personlist [I]. Name + " -- " ); Console. writeline (personlist [I]. Age); console. writeline ( " ================================== " );}}
Check the Il Code as follows:
1 . Method Private Hidebysig Static Void Main () Cel Managed 2 { 3 . Entrypoint 4 // Code size: 166 (0xa6) 5 . Maxstack 3 6 . Locals Init ([ 0 ] Class [Mscorlib] system. Collections. Generic. list' 1 < Class Leleapplication1.person> personlist, 7 [ 1 ] Int32 I, 8 [ 2 ] Class [Mscorlib] system. Collections. Generic. list' 1 < Class Leleapplication1.person> ' <> G _ initlocal0 ' , 9 [ 3 ] Class Leleapplication1.person ' <> G _ initlocal1 ' , 10 [ 4 ] Class Leleapplication1.person ' <> G _ initlocal2 ' , 11 [ 5 ] Bool CS $ 4 $ 0000 ) 12 Il_0000: NOP 13 Il_0001: Newobj Instance Void Class [Mscorlib] 14 System. Collections. Generic. list' 1 < Class Consoleapplication1.person >::. ctor () 15 Il_0006: Stloc.2 16 Il_0007: Ldloc.2 17 Il_0008: Newobj Instance Void Consoleapplication1.person:. ctor () 18 Il_000d: Stloc.3 19 Il_000e: Ldloc.3 20 Il_000f: Ldstr Bytearray ( 71 4e 1e 82 ) // Qn .. 21 // Object initializer _ person: set_name 22 Il_0014: Callvirt Instance Void Leleapplication1.person: set_name ( String ) 23 Il_0019: NOP 24 Il_001a: Ldloc.3 25 Il_001b: LDC. i4.s 22 26 // Object initializer _ person: set_age 27 Il_001d: Callvirt Instance Void Leleapplication1.person: set_age ( Int32 ) 28 Il_0022: NOP 29 Il_0023: Ldloc.3 30 // Call the add method of list <t> to add elements. 31 Il_0024: Callvirt Instance Void Class 32 · [Mscorlib] system. Collections. Generic. list' 1 < Class Consoleapplication1.person> :: Add (! 0 ) 33 // Omitted... 34 } // End of method program: Main
From the 32 rows, we can see that this add method is mainly used here. In the past, adding elements to the list was to manually call this method. Now it is called by the compiler for us. To compile the set initializer successfully, you must meet the following basic conditions:
1. The icollection or generic icollection <t> interface should be implemented to ensure that the collection supports an add method. This is an ideal situation; 2. There is one or more add methods on the ienumerable or generic version of ienumerable <t> interface, even if the required interface is not implemented. This is a relatively loose case; Summary
It can be found that the common characteristics of the Set initializer and object initializer are that they are both compiler techniques. There is no essential difference from the effects produced by previous writing, but we do not know the names of every object generated by the set initiator [the compiler generates the corresponding object names according to its rules, we cannot directly reference].