C # 6.0 new features (reprint)

Source: Internet
Author: User

Brief introduction

The VS 2015 already contains C # 6.0. C # When publishing different versions, C # always has new features, such as dynamic features in linq,c#4.0 in c#3.0, asynchronous operations in c#5.0, and so on.

C # 6.0 adds a lot of new features to help developers better program.


The following example needs to download vs2015 so that there is a c#6.0 environment, the main new features are:

  1. Use the static parameter to refer directly to a method or property in a class without having to take the class name each time.




    {
    Class Program
    {
    static void Main (string[] args)
    {
    WriteLine ("This is the demo for C # 6.0 New Features");

    ReadLine ();
    }
    }
    }

  2. Member properties are automatically initialized, and when a property is defined, the value of the property can be initialized with simple code instead of being initialized in the constructor.
    public class Employee     
    {
    Public Guid EmployeeId {get; set;} = Guid.NewGuid ();
    public string FirstName {get; set;} = "Mukesh";
    public string LastName {get; set;} = "Kumar";


    public string FullName {get {return string. Format ("{0} {1}", FirstName, LastName); } }


  3. The way the members are initialized in the collection changes to become more intuitive.
    Dictionary<int, string> mydictionary = new Dictionary<int, string> ()             
    {
    [1] = "Mukesh Kumar",
    [2] = "Rahul Rathor",
    [3] = "Yaduveer Saini",
    [4] = "Banke chamber"

  4. String formatting, used previously to use String.Format ("{0}-{1}", V1, V2); To format the output, use numbers, error-prone, and now use variable names directly
    Console.WriteLine ($"The full Name of Employee {FirstName} {lastName}");

  5. In c#6.0, you can use a lambda expression to define a simple function within a line, rather than the traditional need to define a complete function.
    6.0 ago
    public static string Getfullname (String firstName, String lastName)         
    {
    return string. Format ("{0} {1}", FirstName, LastName);
    }
    6.0 in
    public static string Getfullname (String firstName, string lastName) = FirstName + "" + lastName;

  6. Before c#6.0, a property needs to define both the get and set methods, even if the set method is not required at all, such as a read-only property. In c#6.0, you can define just one get method and set the initial value of the property
    String FirstName {get;} = "Mukesh";

  7. exception handling improvements, in previous exception handling, you need to judge each outlier in a catch block, then execute different code, and in c#6.0, you can define multiple catch blocks to handle different code based on different outliers.
    try 
    {
    throw new Exception (errorcode.tostring ());
    }
    catch (Exception ex) when (ex. Message.equals ("404"))
    {WriteLine ("This is Http Error");
    catch (Exception ex) when (ex. Message.equals ("401"))
    {WriteLine ("This is unathorized Error");
    catch (Exception ex) when (ex. Message.equals ("403"))
    {WriteLine ("Forbidden"); }

  8. Null value improvement, the previous operation of a variable needs to determine whether NULL, otherwise the program throws an exception; in c#6.0, you can use a simple question mark? Null is automatically determined, and if so, the null value is output.
    //            
    //
    WriteLine (employees. FirstOrDefault ()?. Name);

    //
    WriteLine (employees. FirstOrDefault ()?. Name?? "My Value");

  9. Variable declaration improvements, which previously declared a variable to return a value from a function, need to be defined outside the function, and in c#6.0, the variable can be defined directly in the function call.
    static void Main (string[] args)         
    {
    if (int. TryParse ("+", out var result)) {
    return result;
    }
    return 0; Result is an out of scope
    }

Specific content and sample code can be consulted:

Http://www.codeproject.com/Articles/1070659/All-About-Csharp-New-Features

C # 6.0 new features (reprint)

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.