Microsoft updated the C # language to 6.0 in Visual Studio 2015, adding a lot of good features to keep the C # language in the ranks of the best languages. Here's a quick example of the new C # 6.0 feature, which is tested in the VS2015 preview, and the official version may also add new features.
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using System.console;namespace csharp6research{//Fractional public class fraction {public int A {get; set;} public int B {get; set;} = 1; public string Separator {get;} = "/"; public string Separatorspaces {get;} = String. Empty; public double Value = = (double) A/b; public int This[int Index] = index = = 0? A:B; public int this[string index] = = Index = = "A"? A:B; public override string ToString () = "\{a}\{separatorspaces}\{separator}\{separatorspaces}\{b}"; public void Print () = WriteLine (ToString ()); Public fraction () {} public fraction (int a, int b) {a = A; b = b; } public fraction (int A, int. B, String separatorspaces): This (A, b) {separatorspaces = Separato Rspaces; if (string.IsNullOrEmpty (separatorspaces)) {throw new ArgumentNullException (Nameof (separatorspaces)); }} public static readonly dictionary<string, fraction> commonfractions = new Dicti Onary<string, fraction> {["zero"] = new fraction (), ["one"] = new fraction (1, 1), ["half"] = new fraction (1, 2), ["quarter"] = new fraction (1, 4), ["in Finity "] = new fraction (1, 0),}; } public struct Fractionstruct {public int A {get;} public int B {get;} Public fractionstruct (int a, int b) {a = A; b = b; Public fractionstruct (): This (0, 1) {} public override string ToString () = "\{a}/\{b}"; } class Program {static void Main (string[] args) {foreach (var f in Fraction.commonfracti ONS) {WriteLine ("\{f.key}: \{f.value.value}"); } var fraction = new fraction (1, 3, ""); Fraction. Print (); try {fraction = new fraction (1, 2, NULL); } catch (ArgumentNullException e) if (E.paramname = = "Separatorspaces") {WriteLine (" Separatorspaces can not be null "); } fraction v; Fraction.CommonFractions.TryGetValue ("Harf", out v); V?. Print (); var a = v?. A WriteLine (A = = null); var B = v? ["B"]; WriteLine (b = = null); WriteLine (v?. ToString () = = null); WriteLine (New Fractionstruct (). ToString ()); WriteLine (Default (fractionstruct). ToString ()); } }}
The results of the operation are as follows
Zero:0one:1half:0.5quarter:0.25infinity: ∞1/3separatorspaces can not be nulltruetruetrue0/10/0
1. Auto-property initializers Automatic property initializer
public int B {get; set;} = 1;
You can assign a value to an automatic property directly, and it doesn't have to be written in the constructor.
2. Getter-only Auto-properties read-only automatic properties
public string Separatorspaces {get;} = String. Empty;
Read-only automatic attributes can be initialized directly or initialized in constructors.
3. Expression-bodied members Expression body
public double Value = = (double) A/b;
public int This[int Index] = index = = 0? A:B;
public void Print () = WriteLine (ToString ());
Read-only properties, read-only indexers, and methods can all use lambda expressions as body.
4. String interpolation embedded value
"\{a}\{separatorspaces}\{separator}\{separatorspaces}\{b}";
The expression in the curly braces after the backslash evaluates the value at run time and embeds it in the string. Like in Swift, Swift is a backslash with parentheses, and C # is a backslash.
5. nameof operator nameof operator
throw new ArgumentNullException (Nameof (separatorspaces));
Nameof returns a variable, parameter, or member name.
This is useful when you write a property change notification for a viewmodel layer in WPF, you need to write a string, or use a Help method in a library such as Mvvmlight, you can pass in the property directly, but because it is parsed at run time, there is a slight performance penalty. Now, with the nameof operator, the security and readability of the refactoring can be guaranteed and performance guaranteed.
6. Dictionary initializer dictionary initializer
New dictionary< string, fraction>
{
["zero"] = new fraction (),
["one"] = new fraction (1, 1),
["half"] = new fraction (1, 2),
["Quarter"] = new fraction (1, 4),
["infinity"] = new fraction (1, 0),
};
Dictionaries can now be initialized in a more readable way, with the square brackets enclosing a key equal to value.
7. Parameterless struct Ctors parameterless struct constructor
Public fractionstruct (int a, int b) {a = A; b = b; }
Public fractionstruct (): This (0, 1) {}
Structs can supply custom parameterless constructors.
New Fractionstruct ()
Default (Fractionstruct)
New is to call the parameterless constructor.
The default is to not call the parameterless constructor.
8. Exception Filters Exception Filter
catch (ArgumentNullException E) if (E.paramname = = "Separatorspaces")
{
WriteLine ("separatorspaces can not is null");
}
Sets the condition to enter the catch block.
9. Null propagation Null propagation
V?. A
V? ["B"]
V?. ToString ()
object is null when the property is not called, indexers, methods, and so on, the expression returns NULL, similar to the usage in swift.
The using static members using statically import
Using System.Console;
WriteLine ("separatorspaces can not is null");
You can specify a static class in the using, and then you can use the static method of the class directly in the code that follows, similar to the usage in Java.
Await in catch/finally catch and finally block
Examples are as follows,
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.
}
Experience the new C # 6.0 feature with an example