我盡我所能檢查null值。但通常,我會用is-Operator 嘗試這個。雖然這,我希望,true如果運算元是一個not-null值,他只會回饋。
例子:
EndPoint endPoint = new();
if (endPoint is IPEndPoint)
DoSomething(endPoint as IPEndPoint); // The "as" expressions give the CS8602 warning
...
void DoSomething(IPEndPoint endPoint)
{
...
}
有了這個,我得到一個CS8602編譯器警告,但是endPoint是 aIPEndPoint并且as運算式 can't be null。所以恕我直言,編譯器警告是錯誤的。is在僅與 -Operator進行檢查之前,如何將其靜音?
(也許在里面.editorconfig?)
uj5u.com熱心網友回復:
isandas是兩個單獨的強制轉換操作,編譯器會嘗試強制轉換和驗證它們,所以說 warning 是合理的CS8602。
正如@Adassko 在評論中所說,最好的方法是轉換物件一次并使用轉換結果(如果可轉換):
EndPoint endPoint = new();
if (endPoint is IPEndPoint endpoint)
DoSomething(endpoint);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/513900.html
標籤:C#。网视觉工作室
下一篇:從隔離檔案夾EWS恢復電子郵件
