Possible new features of C # 6.0

Source: Internet
Author: User

Possible new features of C # 6.0

1. Main constructor (Primary constructors)

The main constructor assigns a value to a variable in a class

Before

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

2. Automatic attribute Assignment (auto properties)

before
  Class Point    {public        int X {get; private set;}        public int Y {get; private set;}        Public Point ()        {this            . X = +;            This. Y = +;        }    
After
  Class Point    {public        int X {get; private set;} =        ; public int Y {get; private set;} = 100;    
3. Using static class (static type using statements;)

Using will import all static methods of the reference class into the current namespace

before
Public double A {get {return math.sqrt (Math.Round (5.142));}}
After
Using System.math;...public double A {get {return Sqrt (Round (5.142));}}
4, Property Expressionsbefore
Public double Distance {    get {return math.sqrt ((x * x) + (Y * y));}}
After
public double Distance = MATH.SQRT ((x * x) + (Y * y));

It looks like a lambda expression at first, and it's actually not related to lambda.

5. Method Expressionsbefore
Public point Move (int dx, int dy) {    return new point (X + dx1, Y + dy1);}
After
Public point Move (int dx, int dy) = + new Point (X + dx, Y + dy);

This is similar to the property expressions.

6. Params for Enumerablesbefore
Do (Someenum.toarray ()), ... public void do (params int[] values) {...}
After
Do (someenum);p ublic void do (params ienumerable<point> points) {...}

Previously, the params was only able to modify the parameters of the array type, and now there are a number of types

7, monadic null checking (null check operator) before
if (points! = null) {    var next = points. FirstOrDefault ();    if (next! = null && next. X! = null) return to next. X;}   return-1;
After
var bestvalue = points?. FirstOrDefault ()?. X?? -1;

This is a few lines of code, praise AH

8. Constructor type parameter Inferencebefore
var x = myclass.create (1, "X"); public myclass<t1, T2> create<t1, t2> (T1 A, T2 b) {    return new MYCLASS&L T T1, T2> (A, b);}
After
var x = new MyClass (1, "X");
9. Inline declarations for out params (inline out parameter definition) before
int X;int. TryParse ("123", out X);
After
Int. TryParse ("123", out int x);

To tell the truth, this is very common.

Of course, all of the new features of C # 6.0 are more than that, and they are limited to space, so if you want to learn more, please refer to the links below.

Resources:

Http://roslyn.codeplex.com/wikipage?title=Language%20Feature%20Status&referringTitle=Documentation

Http://en.wikipedia.org/wiki/.NET_Framework_version_history

http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated

Http://www.kunal-chowdhury.com/2012/07/evolution-of-c-10-50-what-are-new.html

Http://www.cnblogs.com/TianFang/p/3647144.html

Possible new features of C # 6.0

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.