c#6.0 New syntax

Source: Internet
Author: User

First, automatic attribute initialization

In previous versions of C #, the properties were written like this:

1  Public int Get Set ; } 2  Public string Get set; }

In c#6.0, an attribute can automatically assign an initial value, for example:

1  Public stringName {Get;Set; } ="Summit";2  Public intAge {Get;Set; } = A;3  PublicDateTime BirthDay {Get;Set; } = DateTime.Now.AddYears (- -);4  Publicilist<int>agelist5 {6       Get;7       Set;8} =Newlist<int> {Ten, -, -, +, -};

Second, import static class

We all know that using a static class method is a class name. The form of a method name, for example:

1 Console.WriteLine ($" before use: {Math.pow (4, 2)}");

The math here is the static class that comes with the framework, and the POW () method must be the same as the code above. In c#6.0, you can use the methods of static classes in the following ways, for example:

1. Use using to import static classes

2, after importing the static class, you can use the same as the normal method, such as:

1 Console.WriteLine ($" before use: {Math.pow (4, 2)}"); 2 Console.WriteLine ($" can be used directly after importing method: {Pow (4, 2)}");

Three, string embedding value

In previous versions, you would use string if you wanted to format the string on the line. Format, for example:

1 Console.WriteLine (string. Format (" Current time: {0}", DateTime.Now.ToString ()));

In c#6.0, it is possible to embed strings directly, for example:

1Console.WriteLine (string. Format ("Current time: {0}", DateTime.Now.ToString ()));2Console.WriteLine ($"Age: {this. Age} Birthday: {this. Birthday.tostring ("Yyyy-mm-dd")}");3Console.WriteLine ($"Age: {{this}. Age}}} Birthday: {{{this}. Birthday.tostring ("Yyyy-mm-dd")}}}");4Console.WriteLine ($"{(this.) Age <= 22? "Small meat" : "Old Meat")}");

Results:

Four, null value operators

As we all know, a null value cannot be called by the ToString () method, and the error "object reference not set to object instance" is reported, for example:

1 string NULL ; 2 Console.WriteLine (name. ToString ());

Results:

There is a null operator in c#6.0, and if it is a null value you can also call the ToString () method, at which point the program will not output anything, for example:

1 int Ten ; 2 Console.WriteLine (Ivalue?). ToString ()); // no need to determine whether it is empty 3 string NULL ; 4 // The program will not error, and will not output any value 5 Console.WriteLine (" null-value operator ");

Results:

This does not need to be the same as before to determine whether the variable is not a null value.

V. Object initializers

In the previous article, I talked about the C # syntax sugar, where I talked about the object initializer and the collection initializer, before initializing the dictionary collection as follows:

1idictionary<int,string> dictold =Newdictionary<int,string>()2 {3{1," First"},4{2,"Second"}5 };6 //Loop Output7 foreach(keyvaluepair<int,string> KeyValueinchdictold)8 {9Console.WriteLine ($"Key:{keyvalue.key},value:{keyvalue.value}");Ten}

Results:

In c#6.0, a field can be initialized by an index, for example:

1idictionary<int,string> dictnew =Newdictionary<int,string>()2 {3[4] =" First",4[5] ="Second"5 };6 //Loop Output7 foreach(keyvaluepair<int,string> KeyValueinchdictnew)8 {9Console.WriteLine ($"Key:{keyvalue.key},value:{keyvalue.value}");Ten}

Results:

Vi. Exception Filters

In previous versions, catching exceptions usually used Try{}catch (), so long as an exception occurred, it would go into the catch, for example:

1 Try 2 {3         Int32.Parse ("we"); 4 }  5catch(Exception ex)6{7         Console.WriteLine (ex); 8 }

Results:

In c#6.0, you can set a condition that satisfies certain conditions in order to enter an exception capture, for example:

1 intExceptionvalue =Ten;2 Try3 {4Int32.Parse ("s");5 }6 Catch(Exception e) when (Exceptionvalue >1)//meet conditions before entering catch7 {8Console.WriteLine ("Catch");9}

Results:

Vii. Expressions of Nameof

We have used typeof,typeof () to get types, nameof expressions and typeof, for example:

1 // get peopletest This string 2 Console.WriteLine (nameof (People)); 3 Console.WriteLine (nameof (Csharpsix));

Results:

Application Scenario: Print the log when the value of the parameter, if the method inside the name of the parameter is changed, and the log is written in the original parameter name, which will lead to inconsistency, using the nameof can ensure that the log inside the parameter name and method inside the parameter name consistent.

Viii. using lambda expressions in attributes/methods

1  Public string string. Format (" name: {0}""Summit"); 2  Public void Print () = Console.WriteLine (Name);

Inside the main () method, call:

1 New csharpsix (); 2 Console.WriteLine (six. NameFormat); 3 Six. Print ();

Results:

c#6.0 New syntax

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.