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/
會在方法結束時結束,如果想提早可以用回之前的方式或者像上面最后那樣寫.