The new features in VS2015 c#6.0

Source: Internet
Author: User

Objective

VS2015 in their own machine is indeed installed, the old strength, want to experience the pleasure of cross-platform, the result was Microsoft mercilessly to a stick, installed or not use, should also be installed Xarmain plug-ins, configuration some parameters bar, because this piece has never been contacted before, Think about it or not to continue to waste time here, so to experience the new features.

My personal blog original link address is http://aehyok.com/Blog/Detail/66.html.

For reference http://roslyn.codeplex.com, refer to PDF document Http://files.cnblogs.com/aehyok/VS2015CSharp6.0.pdf

1. Automatic attribute Enhancement

1.1. Automatic attribute initialization (initializers for auto-properties)

C#4.0 under the resolute realization cannot.

How automatic attributes are initialized in c#6.0

As long as the contact with C # will certainly like this way. It's simple and convenient.

1.2. Read-only property initialization getter-only Auto-properties

Let's take a look at the way we used to do it.

    public class Customer    {public        string Name {get;}        Public Customer (String firstname,string lastName)        {            Name = firstName + "" + LastName;        }    } 

And look at the c#6.0.

    public class Customer    {public        string FirstName {get;} = "Aehyok";        public string LastName {get;} = "Kris";    }  

And the first automatic property initialization is used in the same way.

2. Expression bodied function members

2.1 Using lambda as function body expression bodies on Method-like members

 Public Point Move (intintnew  

Let's take a simple example: a function with no return value

 Public void " " + LastName);

2.2. Lambda expression as an attribute expression bodies on Property-like function members

         Public Override string ToString ()        {            return"" + LastName;        }

Now in C#6

     Public classUser { Public stringFirstName {Get;Set; }  Public stringLastName {Get;Set; }  Public Override stringToString () =string. Format ("{0}--{1}", FirstName, LastName);  Public stringFullName = FirstName +" "+LastName; }

3. Reference static class using static

You can specify a static class in the using, and then you can use the static member directly in the subsequent code

4, empty value judgment null-conditional operators

Look directly at the code and run the results

The result can be found to return null, no longer as cumbersome as before to judge Null.

5. String embedding value

Embed a value in a string

The way it has been used before is

Now we can simply stitch in the following way

6. nameof Expression nameof expressions

In the method parameter check, you may often see this kind of code (used less, this time also learned)

         Public Static void Addcustomer (Customer customer)        {            ifnull)            {                thrownew ArgumentNullException ("customer" );            }        }

There is the customer is our handwritten string, in the name of the customer, it is easy to forget the following string, c#6.0 nameof help us solve this problem, see the new wording

         Public Static void Addcustomer (Customer customer)        {            ifnull)            {                thrownew  ArgumentNullException (nameof (Customer));            }        }

7. Indexed object initializer Index initializers

The initialization of objects directly through the index, the original can actually be implemented

In this way, only three elements in the dictionary can be found, so only the three indexes can access the amount, and other types of objects and collections can be initialized in this way, not listed here.

8. Abnormal filter (Exception filters)

Let's take a look at a transplant method.

            Try            {                varNumbers =Newdictionary<int,string> {[7] ="Seven",[9] ="Nine",[ -] ="Thirteen" }; }            Catch(ArgumentNullException e) {if(E.paramname = ="Customer") {Console.WriteLine ("customer can not is null"); }            }

Another usage is given in Microsoft's documentation, which is thrown to the previous caller when the log record fails

        Private Static BOOLLog (Exception e) {///process some logs            return false; }         Static voidMain (string[] args) {            Try            {                ///            }            Catch(Exception e) {if(!Log (E))        {}} console.readline (); }

9, catch and finally await--await in catch and finally blocks

In c#5.0, the await keyword cannot appear in catch and finnaly blocks. And in 6.0,

            Try{res=awaitResource.openasync (...);//You could does this ....            }            Catch(resourceexception e) {awaitResource.logasync (res, E);//Now the can do ...}finally            {                if(Res! =NULL)                    awaitRes. Closeasync ();//... and this.}

10, parameterless struct constructor--parameterless constructors in structs

Summarize

Before I saw the great God sent an article http://www.cnblogs.com/henryzhu/p/new-feature-in-csharp-6.html, I still can't help but want to experience. It feels good. Also learned a lot of new things.

Personal website Address: aehyok.com

QQ Technology Group Number: 206058845, Verification code: Aehyok

This article links to: http://www.cnblogs.com/aehyok/p/3946286.html

Thank you for your reading, if you are interested in the content of my blog, it is advisable to point a recommendation, thank you for supporting:-O.

The new features in VS2015 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.