[C #6] 7-index initiator,

Source: Internet
Author: User
Tags mscorlib

[C #6] 7-index initiator,
0. Directory

C #6 Add feature catalog

1. Old Version code
1 private static void Main()2 {3     var dictionary = new Dictionary<int, string> {4         { 1, "Value1" },5         { 2, "Value2" },6         { 3, "Value3" }7     };8 }

The Set initialization tool introduced in C #3, but let's use the above syntax to initialize some items when declaring a dictionary or set, in fact, in C #3, this is a syntactic sugar. The actual compiled result is to call the Add method of the dictionary or set to Add these items one by one. But it is a little inintuitive. Let's take a look at this version of IL:

 1 .method private hidebysig static void  Main() cil managed 2 { 3   .entrypoint 4   // Code size       47 (0x2f) 5   .maxstack  4 6   .locals init ([0] class [mscorlib]System.Collections.Generic.Dictionary`2<int32,string> dictionary) 7   IL_0000:  nop 8   IL_0001:  newobj     instance void class [mscorlib]System.Collections.Generic.Dictionary`2<int32,string>::.ctor() 9   IL_0006:  dup10   IL_0007:  ldc.i4.111   IL_0008:  ldstr      "Value1"12   IL_000d:  callvirt   instance void class [mscorlib]System.Collections.Generic.Dictionary`2<int32,string>::Add(!0,13                                                                                                                 !1)14   IL_0012:  nop15   IL_0013:  dup16   IL_0014:  ldc.i4.217   IL_0015:  ldstr      "Value2"18   IL_001a:  callvirt   instance void class [mscorlib]System.Collections.Generic.Dictionary`2<int32,string>::Add(!0,19                                                                                                                 !1)20   IL_001f:  nop21   IL_0020:  dup22   IL_0021:  ldc.i4.323   IL_0022:  ldstr      "Value3"24   IL_0027:  callvirt   instance void class [mscorlib]System.Collections.Generic.Dictionary`2<int32,string>::Add(!0,25                                                                                                                 !1)26   IL_002c:  nop27   IL_002d:  stloc.028   IL_002e:  ret29 } // end of method Program::Main

Essentially, the call to the Add method. C #6 introduces a new method to further optimize this method.

2. Index initializer
1 private static void Main()2 {3     var dictionary = new Dictionary<int, string>4     {5         [1] = "Value1",6         [2] = "Value2",7         [3] = "Value3"8     };9 }

It looks much more intuitive, but it is actually a syntax improvement. The compilation results are also slightly different, as shown below:

 1 .method private hidebysig static void  Main() cil managed 2 { 3   .entrypoint 4   // Code size       47 (0x2f) 5   .maxstack  4 6   .locals init ([0] class [mscorlib]System.Collections.Generic.Dictionary`2<int32,string> dictionary) 7   IL_0000:  nop 8   IL_0001:  newobj     instance void class [mscorlib]System.Collections.Generic.Dictionary`2<int32,string>::.ctor() 9   IL_0006:  dup10   IL_0007:  ldc.i4.111   IL_0008:  ldstr      "Value1"12   IL_000d:  callvirt   instance void class [mscorlib]System.Collections.Generic.Dictionary`2<int32,string>::set_Item(!0,13                                                                                                                      !1)14   IL_0012:  nop15   IL_0013:  dup16   IL_0014:  ldc.i4.217   IL_0015:  ldstr      "Value2"18   IL_001a:  callvirt   instance void class [mscorlib]System.Collections.Generic.Dictionary`2<int32,string>::set_Item(!0,19                                                                                                                      !1)20   IL_001f:  nop21   IL_0020:  dup22   IL_0021:  ldc.i4.323   IL_0022:  ldstr      "Value3"24   IL_0027:  callvirt   instance void class [mscorlib]System.Collections.Generic.Dictionary`2<int32,string>::set_Item(!0,25                                                                                                                      !1)26   IL_002c:  nop27   IL_002d:  stloc.028   IL_002e:  ret29 } // end of method Program::Main

The main difference is that the old syntax is to call the Add method, and the new syntax is the set accesser (set_Item) of the indexer ).

Since it is an index, the index is not only int, but also string, any custom type.

3. Example
Namespace csharp6 {internal class Program {public class Person {public string Name {get; set;} public int Age {get; set;} private Dictionary <string, address >_cache = new Dictionary <string, Address> (); public Address this [string name] {get {return _ cache [name];} set {_ cache [name] = value ;}} public class Address {public string Name {get; set;} public string Zip {get; set ;}} private static void Main () {// string index var colorMap = new Dictionary <string, ConsoleColor> {["Error"] = ConsoleColor. red, ["Information"] = ConsoleColor. yellow, ["Verbose"] = ConsoleColor. white}; // enumeration index var colors = new Dictionary <ConsoleColor, string> {[ConsoleColor. red] = "# F00", [ConsoleColor. green] = "#0F0",}; // The custom type of index supports Person person = new Person {Name = "blackheart", Age = 1, ["home"] = new Address {Name = "Beijing", Zip = "100000"}, ["work"] = new Address {Name = "Nanjing ", zip = "200000" }}; // custom-type index var persons = new Dictionary <Person, List <Address> {[new Person {Name = "blackheart ", age = 1}] = new List <Address> {new Address {Name = "Beijing", Zip = "100000" }}, [new Person {Name = "blackheart ", age = 1}] = new List <Address> {new Address {Name = "Nanjing", Zip = "200000 "}},};}}}
4. Summary

In essence, the syntax [xxx] = yyy can be of any type and can be used for any type supported by the index. Simple and straightforward.

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.