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/

 

會在方法結束時結束,如果想提早可以用回之前的方式或者像上面最后那樣寫.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM