New Language Features in C# 6
Source:https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6
This document describes the new language features in C# 6, the next version of C#. All of these are implemented and available in VS 2015.
Auto-property enhancements Initializers for auto-propertiesYou can now add an initializer to an auto-property, just as you can in a field:
public class Customer { public string First { get; set; } = "Jane"; public string Last { get; set; } = "Doe"; }
The initializer directly initializes the backing field; it doesn’t work through the setter of the auto-property. The initializers are executed in order as written, just as – and along with – field initializers.
Just like field initializers, auto-property initializers cannot reference this – after all they are executed before the object is properly initialized.
Getter-only auto-propertiesAuto-properties can now be declared without a setter.
public class Customer { public string First { get; } = "Jane"; public string Last { get; } = "Doe"; }
The backing field of a getter-only auto-property is implicitly declared as readonly (though this matters only for reflection purposes). It can be initialized through an initializer on the property as in the example above. Also, a getter-only property can be assigned to in the declaring type’s constructor body, which causes the value to be assigned directly to the underlying field:
public class Customer { public string Name { get; }; public Customer(string first, string last) { Name = first + " " + last; } }
This is about expressing types more concisely, but note that it also removes an important difference in the language between mutable and immutable types: auto-properties were a shorthand available only if you were willing to make your class mutable, and so the temptation to default to that was great. Now, with getter-only auto-properties, the playing field has been leveled between mutable and immutable.
Expression-bodied function membersLambda expressions can be declared with an expression body as well as a conventional function body consisting of a block. This feature brings the same convenience to function members of types.
Expression bodies on method-like membersMethods as well as user-defined operators and conversions can be given an expression body by use of the “lambda arrow”:
public Point Move(int dx, int dy) => new Point(x + dx, y + dy); public static Complex operator +(Complex a, Complex b) => a.Add(b); public static implicit operator string(Person p) => p.First + " " + p.Last;
The effect is exactly the same as if the methods had had a block body with a single return statement.
For void-returning methods – and Task-returning async methods – the arrow syntax still applies, but the expression following the arrow must be a statement expression (just as is the rule for lambdas):
public void Print() => Console.WriteLine(First + " " + Last);
Expression bodies on property-like function membersProperties and indexers can have getters and setters. Expression bodies can be used to write getter-only properties and indexers where the body of the getter is given by the expression body:
public string Name => First + " " + Last; public Customer this[long id] => store.LookupCustomer(id);
Note that there is no get keyword: It is implied by the use of the expression body syntax.
Using staticThe feature allows all the accessible static members of a type to be imported, making them available without qualification in subsequent code:
using static System.Console; using static System.Math; using static System.DayOfWeek; class Program { static void Main() { WriteLine(Sqrt(3*3 + 4*4)); WriteLine(Friday - Monday); } }
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/69501.html