1. C # 6.0 Example 1: Automatic attributes support initialization, a new way of embedding strings, referencing static classes by using static, nameof expressions
Csharp6/demo1.xaml.cs
/* * C # 6 Example 1 * Automatic properties support initialization, a new way of embedding strings, referencing static classes by using static, nameof expressions */using system;using system.componentmodel;using windows.ui.xaml;using windows.ui.xaml.controls;using static System.Math; The static class is referenced by using static namespace windows10.csharp6{public sealed partial class Demo1:page {//Auto attribute support initialized public string MyName {get; set;} = "Default value"; Read-only automatic properties can also initialize public int MyAge {get;} = 17; Public Demo1 () {this. InitializeComponent (); This. Loaded + = demo1_loaded; } private void Demo1_loaded (object sender, RoutedEventArgs e) {sample1 (); Sample2 (); Sample3 (); Sample4 (); }//Automatic attribute support initialization (initializers for auto-properties) private void Sample1 () {lblmsg.text = th Is. MyName; Lblmsg.text + = Environment.NewLine; Lblmsg.text + = this. Myage.tostring (); Lblmsg.text + = ENVIRONMENT.NEwline; New Way to//string embedding (string interpolation) private void Sample2 () {//Previous string embedding method LbL Msg.text + = string. Format ("MyName: {0}, MyAge: {1}", this. MyName, this. MYAGE); Lblmsg.text + = Environment.NewLine; New String Embedding method Lblmsg.text + = $ "MyName: {this. MyName}, MyAge: {this. MyAge}, {{this}. MyName}} "; Lblmsg.text + = Environment.NewLine; }//The static class is referenced by using static classes private void Sample3 () {//before passing using static System.Math; reference static State class System.Math//Then you can directly use the System.Math method, as follows Lblmsg.text + = Abs (-100). ToString (); Lblmsg.text + = Environment.NewLine; }//nameof expression private void Sample4 () {datetime datetime = new DateTime (); nameof expression-Used to get the name of the variable, such as the following will output "DateTime", what is the use of this? See the description of the "book" class after Lblmsg.text + = nameof (dateTime); Lblmsg.text + = Environment.NewLine; }//Demonstrate the purpose of the NAMEOF expression public class Book:inotifypropertychanged {public event Pro Pertychangedeventhandler propertychanged; private string _title; public string Title {get {return _title;} set {_title = value; if (propertychanged! = null) {//This is the only way to write propertychanged (this, new Propertych Angedeventargs ("Title")); You can now write to the following propertychanged (this, new PropertyChangedEventArgs (Nameof (Title))); What's the use of it? If I want to modify the name of the property Title, and forget to modify the corresponding name in the PropertyChangedEventArgs, then the compilation will error, in order to modify//certainly modify the property name when it is best to use Visual S The "Rename" method provided by Tudio}}}}}
2. C # 6.0 Example 2: Support await in catch and finally, exception filter
Csharp6/demo2.xaml.cs
/* * C # 6 Example 2 * support await in catch and finally, exception filter */using system;using system.threading.tasks;using windows.ui.xaml;usin G windows.ui.xaml.controls;namespace windows10.csharp6{public sealed partial class Demo2:page {public Dem O2 () {this. InitializeComponent (); This. Loaded + = demo2_loaded; } private void Demo2_loaded (object sender, RoutedEventArgs e) {sample1 (); Sample2 (); }//In Catch and finally also support await in private async void Sample1 () {try { throw new Exception (""); } catch {await task.delay (1000); } finally {await task.delay (1000); }}//exception filter (Exception filters) private void Sample2 () {try { throw new Exception (new Random (). Next (3). ToString ()); } catch (Exception ex) when (ex. Message.equals ("0")//filter exception by when expression {lblmsg.text + = "0"; Lblmsg.text + = Environment.NewLine; } catch (Exception ex) when (ex. Message.equals ("1")//filter exception by when expression {Lblmsg.text + = "1"; Lblmsg.text + = Environment.NewLine; } catch (Exception ex) when (Checkexceptionmessage (ex, "2"))//filter exception by when expression (the judgment condition in the expression can also be a method call) {Lblmsg.text + = "2"; Lblmsg.text + = Environment.NewLine; }} private bool Checkexceptionmessage (Exception Ex, string value) {if (ex. Message.equals (value)) return true; return false; } }}
3. C # 6.0 Example 3: Object initializer with index, null value judgment, lambda expression acting on property or method
Csharp6/demo3.xaml.cs
/* * C # 6 Example 3 * Indexed object initializer, null value determination, lambda expression acting on property or method */using system;using system.collections.generic;using Windows.ui . Xaml;using windows.ui.xaml.controls;namespace windows10.csharp6{public sealed partial class Demo3:page {p Ublic Demo3 () {this. InitializeComponent (); This. Loaded + = demo3_loaded; } private void Demo3_loaded (object sender, RoutedEventArgs e) {sample1 (); Sample2 (); Sample3 (); }//indexed object initializer private void Sample1 () {///Dictionary can also initialize var dict = new Dictionary<int, string> {[7] = "Seven", [9] = "Nine", [13] = "Thirteen"}; Lblmsg.text + = dict[13]. ToString (); Lblmsg.text + = Environment.NewLine; }//null value to determine private void Sample2 () {list<int> List = null; Int? Count= list?. Count; Because list is null, so list? Count is null int? Value3 = list? [3]; Because list is null, so list? [3] is null list = new List<int> {1, 2, 3}; Count = list?. Count; This is an exception because list is not null and the list does not have a 11th element//int? Value10 = list? [10]; Lblmsg.text + = count. ToString (); Lblmsg.text + = Environment.NewLine; The most important application of the null value judgement is this//before the notation of object obj1 = null; if (obj1! = null) {obj1. ToString (); }//The current notation of object obj2 = null; Obj2?. ToString (); }//lambda expression acting on property or method private void Sample3 () {Lblmsg.text + = this. ToString (); Lblmsg.text + = Environment.NewLine; Lblmsg.text + = this. FullName; Lblmsg.text + = Environment.NewLine; public string FirstName {get; set;} = "Lei"; public string Lastname {get; set;} = "Wanglei"; public override string ToString () = $ "{FirstName} {LastName}"; The lambda expression acts on the method public string FullName = = "{FirstName} {LastName}"; Lambda expression acting on property}}
C # 6.0 new features