World War I Windows 10 (43) and World War I 43
[Download source code]
Apsara stack 10 (43)-C #7.0 New Features
Author: webabcd
Introduction
A new feature in C #7.0 of Windows 10
- Introduce new features of C #7.0
Example
1. C #7.0 Example 1: out variable, numeric syntax improvement, asynchronous return of Value Type
CSharp7/Demo1.xaml. cs
/** C #7 example 1 * out variable, numeric syntax improvement, asynchronous return of value type */using System; using System. threading. tasks; using Windows. UI. xaml. controls; namespace Windows10.CSharp7 {public sealed partial class Demo1: Page {public Demo1 () {this. initializeComponent (); sample1 (); sample2 (); sample3 ();} // out variable (out-variables) private void sample1 () {// This is the previous method, the variable string s; OutSample (out s); lblMsg needs to be declared in advance. text + = s; lblMsg. text + = Environment. newLine; // This is written in c #7. You do not need to declare the variable OutSample (out string ss) in advance; lblMsg. text + = ss; lblMsg. text + = Environment. newLine; // This is written in c #7. You do not need to declare variables in advance, and you can use var OutSample (out var sss); lblMsg. text + = sss; lblMsg. text + = Environment. newLine;} private void OutSample (out string str) {str = "xyz";/** Note: * 1. For the out type, it is initialized inside the method, before c #7, it is necessary to declare the external method (this seems unnecessary, so it has been improved today). * 2. For the ref type, it is impossible to change it to the out type, because ref is a reference, its function is to initialize the method externally and change the * //} // numeric syntax improvement (numeric literal syntax improvements) private void sample2 () {int a1 = 123456; int a2 = 123_456; // allows "_" in numbers to improve readability. text + = a1.ToString (); lblMsg. text + = Environment. newLine; lblMsg. text + = a2.ToString (); lblMsg. text + = Environment. newLine; int b1 = 0 xABCDEF; int b2 = 0xAB_CD_EF; // allows "_" in numbers to improve readability. text + = b1.ToString (); lblMsg. text + = Environment. newLine; lblMsg. text + = b2.ToString (); lblMsg. text + = Environment. newLine;} // Value Type Asynchronous return (generalized async return types) private async void sample3 () {lblMsg. text + = (await GetNumber1 ()). toString (); lblMsg. text + = Environment. newLine; lblMsg. text + = (await GetNumber2 ()). toString (); lblMsg. text + = Environment. newLine;} // The int value returned asynchronously before c #7 is written in this way. The Task is a reference type private async Task <int> GetNumber1 () {await Task. delay (100); return 1 ;}// in c #7, int can be returned asynchronously. ValueTask is a value type, which improves efficiency. // note: you must use nuget to reference System. threading. tasks. extensions private async ValueTask <int> GetNumber2 () {await Task. delay (100); return 1 ;}}}
2. C #7.0 Example 2: Reference of value type variables and reference of value type return values, pattern matching, tuples
CSharp7/Demo2.xaml. cs
/** C #7 EXAMPLE 2 * Reference of value type variables and reference of value type return values, pattern matching, tuples */using System; using Windows. UI. xaml. controls; namespace Windows10.CSharp7 {public sealed partial class Demo2: Page {public Demo2 () {this. initializeComponent (); sample1 (); sample2 (); sample3 ();} // reference of value type variables and reference of value type return values (ref locals and returns) private void sample1 () {// example where the value type variable changes to the reference type int a = 1; ref int B = ref a; // Value Type Variable B references the value type variable a = 2; lblMsg. text =. toString (); lblMsg. text + = Environment. newLine; // example of changing the return value of the value type to the reference type int [] array = {1, 2, 3, 4, 5}; ref int x = ref GetByIndex (array, 2); // value type variable x references GetByIndex (array, 2) x = 99; lblMsg. text + = array [2]. toString (); lblMsg. text + = Environment. newLine;} // The returned value type changes to the reference type private ref int GetByIndex (int [] array, int index) {return ref array [index];} // pattern match private void sample2 () {object a = 1; // declare int B, if a is of the int type, a is assigned to B if (a is int B) {lblMsg. text + = B. toString (); lblMsg. text + = Environment. newLine;} switch (a) {// declare int c. If a is of the int type, assign a to c, if c is greater than 0, execute this case int c when c> 0: lblMsg. text + = "case int c when c> 0:" + c; lblMsg. text + = Environment. newLine; break; // declare string c. If a is of the string type, assign a value to c case string c: lblMsg. text + = "case string c:" + c; lblMsg. text + = Environment. newLine; break; }}// Tuples // Note: System must be referenced through nuget. valueTuple private void sample3 () {// The Tuples feature is from System. tuple <T1, T2, T3....> evolved // Note: When multiple return values exist, it is very convenient to use the Tuples feature var user1 = GetUser1 (); lblMsg. text + = $ "{user1.UserId}, {user1.UserName}, {user1.CreateTime}"; lblMsg. text + = Environment. newLine; var user2 = GetUser2 (); lblMsg. text + = $ "{user2.UserId}, {user2.UserName}, {user2.CreateTime}"; lblMsg. text + = Environment. newLine; var user3 = GetUser3 (); lblMsg. text + = $ "{user3.Item1}, {user3.Item2}, {user3.Item3}"; lblMsg. text + = Environment. newLine; (int UserId, string UserName, DateTime CreateTime) = GetUser1 (); lblMsg. text + = $ "{UserId}, {UserName}, {CreateTime}"; lblMsg. text + = Environment. newLine; var obj1 = (UserId: 1, UserName: "webabcd"); lblMsg. text + = $ "{obj1.UserId}, {obj1.UserName}"; lblMsg. text + = Environment. newLine; var obj2 = (1, "webabcd"); lblMsg. text + = $ "{obj2.Item1}, {obj2.Item2}"; lblMsg. text + = Environment. newLine; (int id, string name) = (1, "webabcd"); lblMsg. text + = $ "{id}, {name}"; lblMsg. text + = Environment. newLine;} private (int UserId, string UserName, DateTime CreateTime) GetUser1 () {return (1, "webabcd", DateTime. now);} private (int UserId, string UserName, DateTime CreateTime) GetUser2 () {return (UserId: 1, UserName: "webabcd", CreateTime: DateTime. now);} private (int, string, DateTime) GetUser3 () {return (1, "webabcd", DateTime. now );}}}
3. C #7.0 Example 3: The expression throws an exception. The lambda expression acts on the constructor or attribute.
CSharp7/Demo3.xaml. cs
/** C #7 example 3 * The expression throws an exception. The lambda expression acts on the constructor or attribute, and the local function */using System; using Windows. UI. xaml. controls; namespace Windows10.CSharp7 {public sealed partial class Demo3: Page {public Demo3 () {this. initializeComponent (); sample1 (); sample2 (); sample3 ();} // The expression throws an exception (throw expressions) private void sample1 () {try {string a = null; // supports throwing an exception in the expression string B = ?? Throw new Exception ("ex");} catch (Exception ex) {lblMsg. text + = ex. toString (); lblMsg. text + = Environment. newLine ;}// lambda expressions act on constructors or attributes (more expression-bodied members) // note: in c #6, lambda expressions are supported to act on fields or methods. private void sample2 () {MyClass obj = new MyClass ("webabcd"); lblMsg. text + = obj. text; lblMsg. text + = Environment. newLine;} public class MyClass {private string _ text; public MyClass (String text) => _ text = text; // The lambda expression acts on the public string Text of the constructor. // The lambda expression acts on the attribute {get => _ text; set => _ text = value ?? "Default text" ;}}// local functions private void sample3 () {int a = GetNumber (); lblMsg. text + =. toString (); lblMsg. text + = Environment. newLine; // supports the int GetNumber () {return 1 ;}}} partial function ;}}}}
OK
[Download source code]