1. The static using (static using)
A static using declaration allows a direct call to a static method without using the class name.
The static using declaration allows invoking static methods without the class
name.
In C # 5
using System;
Console.WriteLine ("Hello, world!");
In C # 6
using static system.console;
WriteLine ("Hello, World");
2. Expression method (Expression-bodied Methods)
With an expression method, only one of the statements can be written using lambda syntax.
With expression-bodied methods, a and that includes just one statement can is written with the
Lambda syntax.
in C # 5 public
bool Issquare (Rectangle rect)
{return
rect. Height = = rect. Width;
}
In C # 6 public
bool Issquare (Rectangle rect) => rect. Height = = rect. Width;
3. Expression attributes (Expression-bodied properties)
Similar to the expression method, only one-line property of a get accessor can be written using lambda syntax.
Similar to Expression-bodied methods, One-line properties and only a get accessor
can is written with the Lambda Synt Ax in
C # 5 public
string FullName
{get {return
FirstName + "" + LastName;
}
}
In C # 6 public
string FullName => FirstName + "" + LastName;
4. Automatic attribute initializers (auto-implemented property intializers)
Automatic properties can be initialized using the property initializer.
Auto-implemented properties can be initialized with a property initializer.
In C # 5 public class Person {public person
()
{age
=;
}
public int Age {get; set;}
}
In C # 6 public
class Person {public int age {get
; set;} = N
5. Read-only automatic properties (read-only Auto Properties)
C # 5 requires complete property syntax to implement read-only properties, and C # 6 can be implemented using automatic properties.
To implement Read-only properties, C # 5 requires the full property syntax.
with C # 6, you can doing this using auto-implemented properties.
In C # 5
private readonly int _bookid;
Public BookID
{
get
{
_bookid
}
}}
In C # 6 public
BookID {get;}
6. Nameof operator (nameof Operator)
The name of fields, properties, methods, and types can be accessed through nameof. Using nameof, you can easily refactor the name change.
With the new nameof operator, names of fields, properties, methods, or types can is
accessed. With this, the name changes are not missed with refactoring.
In C # 5 public
void Method (Object o)
{
if (o = = null) throw new ArgumentNullException ("O");
In C # 6 public
void Method (Object o)
{
if (o = = null) throw new ArgumentNullException (Nameof (o));
7. Null transfer operator (Null propagation Operator)
Null-passing operators simplify null-value checking.
The null propagation operator simplifies null checks.
In C # 5
int? age = P = = null? null:p.age;
var handler = Event;
if (handler!= null)
{
handler (source, E);
}
In C # 6
int? age = P?. Age;
Handler? Invoke (source, E);
8. String interpolation (interpolation)
The string difference is removed from the pair string. A call to format that replaces the number format placeholder with an expression placeholder.
The string interpolation removes calls to string. Format. Instead of using
numbered format placeholders in the string, the placeholders can include
expressions.
In C # 5 public
override ToString ()
{return
string. Format ("{0}, {1}", Title, Publisher);
}
In C # 6 public
override ToString () => $ "{Title} {Publisher}";
9. Dictionary initializers (Dictionary initializers)
Dictionaries can be initialized with dictionary initializers that resemble collections.
Dictionaries can now is initialized with a dictionary initializer-similar to the
collection initializer.
In C # 5
var dict = new Dictionary<int, string> ();
Dict. ADD (3, "three");
Dict. ADD (7, "seven");
In C # 6
var dict = new Dictionary<int, string> ()
{
[3] = "three",
[7] = "Seven"
};
10. Abnormal filter (Exception Filters)
The exception filter allows you to filter before catching an exception.
Exception filters allow you to filter exceptions before catching.
In C # 5
try
{
//etc.
} catch (MyException ex)
{
if ex. ErrorCode!= 405) throw;
etc.
}
In C # 6
try
{
//etc.
} catch (MyException ex) when (ex. ErrorCode = = 405)
{
//etc.
}
11. Using Await (Await in catch) at catch
Await can be used directly in a catch block, which is required for use in C # 5.
Await can now is used in the catch clause. C # 5 required a workaround.
In C # 5
bool haserror = false;
String errormessage = null;
Try
{
//etc.
} catch (MyException ex)
{
haserror = true;
ErrorMessage = ex. message;
}
if (haserror)
{
await new messagedialog (). Showasync (errormessage);
}
In C # 6
try
{
//etc.
} catch (MyException ex)
{
await new messagedialog (). Showasync (ex. message);
}
The above is a small set to introduce the C # 6.0 new features of the summary, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!