[英]C# - Why is VS 2017 greying out null check on event object?
對事件對象執行空檢查時,Visual Studio會將代碼顏色更改為淺灰色。 如果我沒記錯的話,通常意味着這行是不必要的。 顯然,在我的情況下不是這樣(或者是?)。 請參閱下面的示例:
public event PropertyChangedEventHandler PropertyChanged;
string test;
protected void OnPropertyChanged (string propertyName)
{
if (PropertyChanged != null) { //This line is grey
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
} //This line is grey
if (test != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
如果右鍵單擊並查看( 快速操作和重構 )建議的修復程序,您將看到可以通過使用?
在與引發事件相同的行上執行null
引用檢查?
操作員(安全導航操作員)。
建議采取的措施如下:
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.