New language features in c#6.0

Source: Internet
Author: User
Tags dotnet

Sun Guangdong 2015.6.5

What ' s New in C # 6:

Http://channel9.msdn.com/Shows/Visual-Studio-Toolbox/Whats-New-in-C-6

Cross Platform Development Series:
Http://channel9.msdn.com/Shows/CZSK-videa/Cross-Platform-Development-1-Introduction

Developer Productivity:what ' s New in C # 6 Series:
Http://channel9.msdn.com/Series/Developer-Productivity-Whats-New-in-C-6/01

Open source code for. NET Compiler Platform ("Roslyn"):
Https://github.com/dotnet/roslyn

New Language Features in C # 6 article:
Https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6



New language features in c#6.0: This article describes the new language features in c#6.0. All of this can be done in VS 2015.
1, Auto-property enhancements automatic Property Enhancement 1.1 Auto-attribute initial value setting, you can now add an automatic attribute to the initial value setting, just like the field: public class Customer
{
public string First {get; set;} = "Jane";
public string Last {get; set;} = "Doe";
}


The initializer is executed in the order in which it was written, just like a normal field initializer, and an automatic property initializer cannot refer to the value of this


Content – After all, they perform initialization before the object.


1.2 Automatic properties for read-only properties public class Customer
{
public string First {get;} = "Jane";
public string last {get;} = "Doe";
}
People will wonder how the assignment was made before the read-only attribute. In the constructor:
public class Customer
{
public string Name {get;};
Public Customer (string first, String last)
{
Name = First + "" + Last;
}
}




Expression-function Body Member (expression-bodied function members): A lambda expression can be declared as an expression body and composed of regular function body blocks. This feature can bring the same convenience function type members.


Expression bodies on Method-like members:
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;


public void Print () = Console.WriteLine (First + "" + last);




Expression bodies on Property-like function members:
Properties and Indexers:
public string Name = + First + "" + Last;
Public Customer This[long id] \ = store. Lookupcustomer (ID);




Using static: This feature allows you to import all static members of the type you want to access, so that they can be used in the following code without qualification:
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);
}
}


This is great when you have a set of related features for a specific domain and it is ready to use. System.Math will be a common example. It also allows you to directly visit


A value that asks a single enumeration type, such as a member of System.dayofweek.




Extension methods: The extension method is a static method, but should be used as an instance method. Instead of the global scope extension method, use static attributes to make the type's extender


method is available for extension methods:
Using static System.Linq.Enumerable; The type, not the namespace
Class Program
{
static void Main ()
{
var range = Range (5, 17); Ok:not extension
var odd = Where (range, i = i% 2 = = 1); Error, not in scope
var even = range. Where (i = = i% 2 = = 0); Ok
}
}






Null-conditional operators sometimes code is often done with a null check. The null condition operator allows you to access a member, providing an empty result only if the recipient is a non-null element:
Int? Length = customers?. Length; NULL if customers is NULL
Customer first = customers?  [0]; NULL if customers is NULL


Null condition operator for convenience, with empty merge operator?? Used together:
int length = customers?. Length?? 0; 0 if customers is null


An empty condition operator, which has a short-circuit behavior, that is, the chain member immediately following it is accessed when the preceding content is not empty.
Int? First = customers? [0]. Orders.count ();
This example is essentially equivalent to:
Int? First = (Customers! = NULL)? Customers[0]. Orders.count (): null;


Of course the null condition operator itself can be linked if there is a need to check the null chain more than once:
Int? First = customers? [0]. Orders?. Count ();


Please note that? The call after the operator (the list of arguments enclosed in parentheses) cannot be executed immediately – this results in too much syntactic ambiguity. Therefore, the direct adjustment


With a delegate, if it exists, only does not work the way. However, you can call the delegate method by calling:
if (predicate?. Invoke (e)?? False) {...}


We expect that a very common use of this pattern will be on the triggering event:
PropertyChanged?. Invoke (this, args);


String interpolation strings interpolation: String.Format is very flexible and useful, but they are a bit clumsy and prone to error. Especially unfortunate is the use of placeholders such as {0}


The format string, which must be supplied on a separate line of arguments:
var s = String.Format ("{0} is {1} year{{s}} old", P.name, P.age);


String interpolation, you can put the expression in the correct place, through "holes" directly in the string:
var s = $ "{P.name} is {p.age} year{{s}} old";


Just like String.Format, the optional alignment and format specifier can be:
var s = $ "{p.name,20} is {p.age:d3} year{{s}} old";


Content can be almost any expression, even including other strings:
var s = $ "{P.name} is {p.age} year{(p.age = = 1?) "": "S")} old ";


Notice that the conditional expression was parenthesized, so the the: "s" doesn ' t get confused


With a format specifier.
Note that it is a conditional expression with parentheses, so that: "s" does not clutter the format specifier.




Nameof Expressions: (if x = = null) throw new ArgumentNullException (nameof (x));
WriteLine (nameof (person). Address.zipcode)); Prints "ZipCode"




Index initializers: Object and collection initializers can be used to initialize fields and properties declaratively, or to set a set of elements. Initializes the dictionary with an indexer and its


His object is not elegant. We are adding the new object initializer syntax to allow you to set the value of the key through any indexer, its new object:
var numbers = new Dictionary<int, string> {
[7] = "Seven",
[9] = "Nine",
[] = "Thirteen"
};




Exception Filters function Early VB has. F # has. Now there are in C #.
try {...}
catch (MyException E) when (Myfilter (e))
{
...
}
private static bool log (Exception e) {/* log it */; return false;}
...
try {...} catch (Exception e) when (Log (e)) {}




Await in Catch and finally blocks:

Resource res = null;
Try
{
res = await resource.openasync (...); You could does this.
...
}
catch (Resourceexception e)
{
Await Resource.logasync (res, E); Now the can do ...
}
Finally
{
if (res! = null) await res. Closeasync (); ... and this.
}


Extension ADD Methods in collection initializers:
Improved overload resolution:


New language features in 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.