using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Language._7._0 { public class Class1 { private string label; public static void run() { //out 变量不需要在方法之外事先声明answer if (int.TryParse("123", out var answer)) Console.WriteLine(answer); else Console.WriteLine("Could not parse input"); //元组,需要引入包System.ValueTuple var alphabetStart = (Alpha: "a", Beta: "b"); Console.WriteLine($"{alphabetStart.Alpha}, {alphabetStart.Beta}"); (int max, int min) = Range(); Console.WriteLine(max); Console.WriteLine(min); //数字文本和分隔符 int Sixteen = 0b0001_0000; int ThirtyTwo = 0b0010_0000; long BillionsAndBillions = 100_000_000_000; //默认文本表达式,此处不赋值则编译不通过 Func<string, bool> whereClause=default; Console.WriteLine($"默认文本表达式{whereClause}"); //推断元组元素名称 int count = 5; string label = "Colors used in the map"; var pair = (count, label); // element names are "count" and "label" } //ValueTask如果返回Task<T>,你需要额外的对象分配操作。使用ValueTask则不用 public async ValueTask<int> Func() { await Task.Delay(100); return 5; } private static (int max, int min) Range() { return (max: 5, min: 2); //或者 //return (5, 2); } //在 C# 7.0 中,你可以在属性 和索引器 上实现构造函数 、终结器 以及 get 和 set 访问器 public string Label { get => label; set => this.label = value ?? "Default label"; } } }