c# 8.0

1. nullable string

从前 string 一定是 nullable. 现在则不一定

string? name = null;  要加 ? 才可以表示 nullable 

限制泛型不能 null

public void Abc<T>(T xyz) where T : notnull { }

public void OnGet()
{
    string? w = null;
    Abc(w); // warming
}

读的时候 notnull 但是 set 的时候可以 null 

[AllowNull]
public string MyValue
{
    get
    {
        return _innerValue ?? string.Empty;
    }
    set
    {
        _innerValue = value;
    }
}
private string? _innerValue { get; set; }

public void OnGet()
{
    MyValue = null;
    string xxx = MyValue;
}

2. range operation 

以前最讨厌翻译 js -> c# 遇到 substring 

因为 js 的 substring 是 start, end 

c# 的是 start, length 

每次都要改成 start, end - start 

现在可以直接 value[start..end] 结果就和 js 一样了。

3. 短的 using

refer : https://csharp.christiannagel.com/2019/04/09/using/

会在方法结束时结束,如果想提早可以用回之前的方式或者像上面最后那样写.

原文地址:https://www.cnblogs.com/keatkeat/p/12155974.html