C #6.0 function Preview (1)

Source: Internet
Author: User

I. initialize the members and elements of the index. 1.1 think about the original initialization set Dictionary. The unit test below assigns a [TestMethod] public void DictionaryIndexWithoutDotDollar () value to a set through the set initialization tool () {Dictionary <string, string> builtInDataTypes = new Dictionary <string, string> () {"Byte", "0 to 255" },{ "Boolean ", "True or false. "}, {" Object "," An Object. "}, {" String "," A string of Unicode characters. "}, {" Decimal "," ± 1. 0 × 10e-28 to ± 7. 9 × 10e28 "}};} 1.2 key-value pairs begin set Diction Although the Code above is a bit obscure, it is still a set of key-value pairs. If the syntax is in the form of <index >=< value>, it is clearer and easier to understand. In C #6.0, you can use the C # object initialization tool and a new index member syntax. Initialization Based on Integer elements: var cppHelloWorldProgram = new Dictionary <int, string> {[10] = "main (){", [20] = "printf (\" hello, world \ ")", [30] = "}"}; Assert. areEqual (3, cppHelloWorldProgram. count); Note: although the instantiation Code uses integers as the index, Dictionary <TKey, TValue> supports any type of index (as long as the index supports IComparable <T> ). The following describes how to use a string as the index type and specify the element value Dictionary <string, string> builtInDataTypes = new Dictionary <string, string> {["Byte"] = "0 to 255 ",//... // Error: mixing object initializers and // collection initializers is invalid // {"Boolean", "True or false. "}, [" Object "] =" An Object. ", [" String "] =" A string of Unicode characters. ", [" Decimal "] =" ± 1. 0 × 10e? 28 to ± 7. 9 × 10e28 "}; 1.3 operator $ initialize the set Dictionary with the new index member initializer there is a new operator" $ "(isn't it inspired by ps, ). The string index member syntax is provided for string-based indexing. Use this new syntax, more like a dynamic member call, rather than the representation of the above string. The following is an example of [TestMethod] public void DictionaryIndexWithDotDollar () {Dictionary <string, string> builtInDataTypes = new Dictionary <string, string> {$ Byte = "0 to 255 ", // Using indexed members in element initializers //... $ Boolean = "True or false. ", $ Object =" An Object. ", $ String =" A string of Unicode characters. ", $ Decimal =" ± 1. 0 × 10e? 28 to ± 7. 9 × 10e28 "}; Assert. areEqual ("True or false. ", builtInDataTypes. $ Boolean);} copy the code to understand the "$" operator. Pay attention to the AreEqual method called. Have you noticed that the builtInDataTypes variable calls the "$ Boolean" member of the dictionary, but there is no "Boolean" member in the dictionary. Because the operator "$" calls the index member in the dictionary, it is equivalent to buildInDataTypes ["Boolean"]. Therefore, when using the operator "$", you do not need to explicitly specify the index. As a string-based operation, the compilation does not verify whether the string index exists in dictionary. That is, as long as it is a valid C # member (case sensitive) in the operator "$" ($ + "C # member "). The even more surprising index member syntax is that the advantage of string indexes in weak data types (such as XML, JSON, CSV, and even database searches) is considered. The following is an example of using string index members conveniently using the Newtonsoft. Json framework. [TestMethod] public void initialize () {// Additional data types eliminated for elucidation string jsonText = @ "{'byte': {'keyword': 'byte', 'dotnetclassname ': 'byte', 'description': 'unsigned integer ', 'width': '8', 'range': '0 to 255'}, 'boolean ': {'keyword': 'bool ', 'dotnetclassname': 'boolean', 'description': 'logical Boolean type', 'width': '8', 'range ': 'true o R false. '},} "; JObject jObject = JObject. parse (jsonText); Assert. areEqual ("bool", jObject. $ Boolean. $ Keyword) ;}at the end, note that the example may not be very obvious. The above operator "$" syntax only applies to the case where the index is of the string type (such as Dictionary <string,…>) Ii. Automatic attribute initialization class is always annoying. For example, a simple custom set type (for example, Queue <T>) maintains a List of private System. Collections. Generic. List <T> attributes within it. When instantiating a set, you must initialize the queue containing the list. However, for an attribute, a reasonable solution is that a supported field requires an initiatator or other constructor, however, the amount of code in this combination will almost double. In C #6.0, There is a shortcut: Automatic attribute initialization. Now you can specify direct initialization. The Code is as follows: internal class Queue <T> {private List <T> InternalCollection {get ;}= new List <T>; // Queue Implementation //...}

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.