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:
- 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 ();
}
}
}
- 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); } }
- 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"
- 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}");
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 agopublic 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;
- 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";
- 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"); }
- 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");
- 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)