Contents

C#8新增语法

Using 声明

Using 声明就是对using()语法的简写

当函数执行完毕时 会调用 对象的 Dispose方法 释放对象

在使用using语法时,声明的对象必须继承System.IDisposable接口

因为必须具备Dispose方法,所以当声明没有继承该接口的对象时会报错

using StreamWriter s2 = new StreamWriter("文件路径");
//对该对象进行逻辑操作
s2.Write(5);
s2.Flush();
s2.Close();
//利用这个写法 就会在上层语句块执行结束时释放该对象

静态本地函数

静态本地函数就是在本地函数前方加入静态关键字

它的作用就是让本地函数不能够使用访问封闭范围内(也就是上层方法中)的任何变量

public int CalcInfo(int i)
{
    bool b = false;
    i += 10;
    Calc(ref i, ref b);
    return i;
    static void Calc(ref int i, ref bool b)
    {
        i += 10;
        b = true;
    }
}

Null 合并赋值

回顾空合并操作符:左边值 ?? 右边值

如果左边值为null 就返回右边值 否则返回左边值

空合并赋值是C#8.0新加的一个运算符 ??=

类似复合运算符 左边值 ??= 右边值

当左侧为空时才会把右侧值赋值给变量

string str = null;
str ??= "4565";
print(str);//4565
str ??= "1111";
print(str);//4565

解构函数Deconstruct

解构函数Deconstruct (C# 7就有了)

我们可以在自定义类当中声明解构函数

这样我们可以将该自定义类对象利用元组的写法对其进行变量的获取

一个类中可以有多个Deconstruct,但是参数数量不能相同

语法: 在类的内部申明函数public void Deconstruct(out 变量类型 变量名, out 变量类型 变量名…..)

public class Person
{
    public string name;
    public bool sex;
    public string number;
    public string email;

    //可以使用元组来进行赋值
    public void Deconstruct(out string n, out bool sex) => (n, sex) = (this.name, this.sex);

    public void Deconstruct(out string n, out bool sex, out string number, out string email)
    {
        n = name;
        sex = this.sex;
        number = this.number;
        email = this.email;
    }
}
Person p = new Person();
p.name = "路飞";
p.sex = false;
p.email = "11111@163.com";
p.number = "11111";

(string name, bool sex) = p;
print(name);
print(sex);
string str3;
(_, _, _, str3) = p;
print(str3);//11111@163.com

模式匹配增强功能——switch表达式

switch表达式是对有返回值的switch语句的缩写

用=>表达式符号代替case:组合

用_弃元符号代替default

public Vector2 GetPos(PosType type) => type switch
{
    PosType.Top_Left => new Vector2(0, 0),
    PosType.Top_Right => new Vector2(1, 0),
    PosType.Bottom_Left => new Vector2(0, 1),
    PosType.Bottom_Right => new Vector2(1, 1),
    _ => new Vector2(0, 0)
};

模式匹配增强功能——属性模式

就是在常量模式的基础上判断对象上各属性

用法:变量 is {属性:值, 属性:值}

 DiscountInfo info = new DiscountInfo("5折", true);
 if (info is { discount: "6折", isDiscount: true })
            print("信息相同");

结合switch表达式使用

public float GetMoney(DiscountInfo info, float money) => info switch
{
    //可以利用属性模式 结合 switch表达式 判断n个条件是否满足
    { discount: "5折", isDiscount: true } => money * .5f,
    { discount: "6折", isDiscount: true } => money * .6f,
    { discount: "7折", isDiscount: true } => money * .7f,
    _ => money
};

模式匹配增强功能——元组模式

属性模式我们可以在switch表达式中判断多个变量同时满足再返回什么

但是它必须是一个数据结构类对象,判断其中的变量

而元组模式可以更简单的完成这样的功能,我们不需要声明数据结构类,可以直接利用元组进行判断

int ii = 10;
bool bb = true;
if((ii, bb) is (11, true))
{
    print("元组的值相同");
}

结合switch表达式使用

public float GetMoney(string discount, bool isDiscount, float money) => (discount, isDiscount) switch
{
    ("5折", true) => money * .5f,
    ("6折", true) => money * .6f,
    ("7折", true) => money * .7f,
    _ => money
};

模式匹配增强功能——位置模式

如果自定义类中实现了解构函数

那么我们可以直接用对应类对象与元组进行is判断

public class DiscountInfo
{
    public string discount;
    public bool isDiscount;
    public void Deconstruct(out string dis, out bool isDis)
    {
        dis = this.discount;
        isDis = this.isDiscount;
    }
}
if(info is ("5折", true))
{
    print("位置模式 满足条件");
}

结合switch表达式使用

public float GetMoney2(DiscountInfo info, float money) => info switch
{
    ("5折", true) when money > 100 => money * .5f,
    ("6折", true) => money * .6f,
    ("7折", true) => money * .7f,
    _ => money
};

配合when关键字进行逻辑处理

public float GetMoney2(DiscountInfo info, float money) => info switch
{
    ("5折", true) when money > 100 => money * .5f,
    ("6折", true) => money * .6f,
    ("7折", true) => money * .7f,
    _ => money
};