c#6.0 some features

Source: Internet
Author: User

1. Improvements to Automatic attribute initialization

Can be initialized directly when declaring properties

public int ID {get;set;} = 10;

The automatic attribute eliminates the internal process of get and set, and directly uses set;get;

This process is given to the system, and the system dynamically generates a corresponding field for this property.

The following code is an example of its own control of the Get and set methods:

        private int myVar;        public int MyProperty        {            get {return myVar;}            set {MyVar = value;}        }
Here's the simplest logic.

Demo: You can see that the initial value assigned to the property is valid and the output is correct.

2. Introduction of static Classes

As an example of a console application, the code that was previously written in the console output is:

Console.WriteLine ("Hello World");

The console here is a static class that belongs to the System namespace.

It is now possible to introduce a static class into the namespace where it is introduced.

Using static System.Console;

It is important to note that the full name of the class name of the static class is required using the static keyword.

It can be used in code like this:

WriteLine ("The World, Hello");

The start of a static class is omitted. Personal understanding is for the redundancy of the code.

3.string. Improvements to format

This changes its form directly, and no longer uses string. Format in this form.

To form a comparison, here are two examples of comparisons.

Original:

String name= "like"; string t1=string. Format ("{0,10}", name);

For the understanding of {0,10}, 0 means matching the No. 0 parameter, 10 means the left completion, leaving the string length less than 10 when left with a space complement. If the original string is longer than 10, then this parameter will be invalid, and if it is a negative, then the right complement is indicated.

If {0:YYYY-MM-DD} is separated by a colon, the data is displayed in the following format.

New:

String name= "like"; string t1=$ "{0,10}";

The place directly replaced the string with "$". Format, and the parameters are written in the {} curly braces, and no placeholders are required.

4. Set initializer

I used to initialize this way before,

Dictionary<string,int> dic=new dictionary<string,int>{{"1", 1},{"2", 2}};

Now it can be:

Dictionary<string,int> dic=new dictionary<string,int>{["1"]=1,["2"]=2};

Personal feel convenient a little bit, in the visual aspect is also relatively good-looking.

5.null-expression

Previously only known nullable value types, such as the format of int?, normal, value type is not NULL, only reference type can be null;

String[] test=new string[]{"Xihuan", "Xiaoai"};int? Length1=test[0]?. Length;
Int? RESULT=TEST[1]?. Length??

int d= "SFSD"?. Length?? 5;

?? The double question mark operator with NULL on the left is the right, and the left is not NULL for the left value.

c#6.0 some features

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.