using
//可以使用
using var xxx = new xxx();
xxxx;
//来代替原来的
using (var xxx = new xxx())
{
xxxx;
}
//两者的区别就是新的语法更简洁,using作用域是在函数结束的时候自动释放。
全局using
利用global using 语法可以进行全局using引用,这样就不用每个文件都有using一堆的引用。需要将所有的global using都放在一个文件里,文件名为GlobalUsings.cs。
global using System;
global using System.Collections;
global using System.Collections.Generic;
global using System.IO;
global using System.Linq;
global using System.Text;
global using System.Reflection;
global using System.Text.RegularExpressions;
namespace
文件范围限定namespace,将文件的缩进减少一级。
//可以使用
namespace sourcetest;
public class Command
{
[CommandMethod("Test")]
public void Test()
{
using var tr = new DBTrans();
}
}
//代替原来的
namespace sourcetest
{
public class Command
{
[CommandMethod("Test")]
public void Test()
{
using var tr = new DBTrans();
}
}
}
可空类型
C# 新版本对于null做了很多的语法糖以及可空类型来帮助开发者减少因null处理不当导致的错误。
◦ 首先开启可空类型 enable
◦ 声明可空的变量
string? a = null; // 声明可空的字符串类型
int? a = null; // 声明可空的整形
int b = null; // 报错
string c = null; // 报错