. net/c# variations and derivative points of Knowledge C # 6.0

Source: Internet
Author: User

C # 6.0, corresponding to. NET 4.6, corresponds to VS 2015, new features: main constructor, using static class, property expression, method expression, enumeration parameter, null judgment, Constructor type parameter inference, inline out, Automatic attribute enhancement, string embedding value string. Format becomes a direct "{variable}", an nameof expression, an exception filter, a catch and an await in finally, a parameterless struct-body constructor

1. String embedding value

This resharper automatic prompt, can be converted automatically, but afraid of the technology is too new, not good transplant, or temporarily not in the project

String a = string. Format ("Ab{0}d", "X"); Console.WriteLine (a); string x = "x"; string b = $ "ab{x}d"; Console.WriteLine (b); Console.readkey ();  

In front of the string plus $, the middle with {}, which can put variables or strings, concise a lot.

2. Main constructor

public class Point {    private int x, y;    public point (int x, int y)        this.x = x;        This.y = y;    }} Public equivalent class point (int x, int y) {private int x, y;}      

3. Automatic attribute Assignment

   Class point    {public        int X {get; private set;}        public int Y {get; private set;}        Public Point ()        {this            . X = +;            This. Y = +;}} Equivalent class point {public int X {get; private set;} = n; public int Y {get; private set;} =  

4. Referencing static classes

Used to Math.Round (1.2), each time to use the Math, you can now refer to (using System.Math;), and then each time directly Round () can

5. Attribute expression/Method expression

Public double distance{    get {return 1;}} Equivalent public double Distance1 = 1; 
Public DayOfWeek Day (int i) {    return dayofweek.friday;} Equivalent public DayOfWeek day1 (int i) = Dayofweek.friday; 

6. Null check operator

String a = null;//string x=a.tostring ();//Error//console.writeline (x); string y=a?. ToString (); Console.WriteLine (y); Console.readkey (); 

Before calling the method, from time to time to determine whether it is empty, such as the Common ToString (), generally use convert.tostring (), otherwise prone to error, now as long as the use? ToString () is ready.

7. Inline out

int x; int. TryParse ("123" out x); // equivalent int. TryParse ("123"outint x);

Because out must be defined before use, but the definition is useless, the value should be changed in the method, so it can be directly merged together with

. net/c# variations and derivative points of Knowledge C # 6.0

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.