C#各版本新功能 C#7.1

async main方法

static int Main()//以前
{
    return DoAsyncWork().GetAwaiter().GetResult();
}
static async Task<int> Main() //现在 有返回值
{
    // This could also be replaced with the body
    // DoAsyncWork, including its await expressions:
    return await DoAsyncWork();
}
static async Task Main()//现在 没有返回值
{
    await SomeAsyncMethod();
}

default 文本表达式

Console.WriteLine(default(string)); //output null  引用类型默认null
Console.WriteLine(default(bool));//output false  值类型
Console.WriteLine(default(GUID));//output 00000000-0000-0000-0000-0000000000

Func<string, bool> whereClause = default(Func<string, bool>);//以前
Func<string, bool> whereClause = default; //现在 编译器已经明确知道要传的类型

public static void Method(ImmutableArray<int> array) { }
public static void Main(string[] args)
{
    Method(default);//已知类型
}

//你可以在default(…)表达式可以出现的所有地方使用默认文本表达式:
public static void Main(string[] args = default) {   // 可选参数的默认值
    int i = default;        // 类型 System.Int32 的默认值 0
    string s = default;     // 类型 System.String 的默认值 null
    Method(default);        // 使用参数的默认值调用一个方法
    T t = default;          // 类型参数的默认值
    return default;         // 一个有返回值的方法返回默认值
}
//你还能够在判断条件中使用默认文本表达式
int x = 2;
if (x == default) { }       // 判断x是否是类型 System.Int32 的默认值 0
if (x is default) { }       // 同上

推断元组元素名称

int   count =   5 ;
string   label =   "Colors used in the map" ;
var   pair = (count, label);   // element names are "count" and "label"

泛型类型参数下的模式匹配

is 和 switch 类型模式的模式表达式的类型可能为泛型类型参数。 这可能在检查 struct 或 class 类型且要避免装箱时最有用

编译器有 -refout 和 -refonly 两个选项,可用于控制引用程序集生成

原文地址:https://www.cnblogs.com/maanshancss/p/7e6e758c112ea757abeb8750d2b0f664.html