共用方式為


移除不必要的隱藏運算子 (IDE0080)

財產 價值
規則標識碼 IDE0080
標題 移除不必要的抑制運算符
類別 風格
子類別 不必要的程式碼規則(表達式層級喜好設定)
適用的語言 C#

概述

此規則會標幟不必要的 抑制或允許空值的運算子。 當運算符在沒有任何作用的情境中使用時,運算符是不必要的。 例如,使用抑制運算子 x!,表示參考型別的運算式 x 不是 null。 不過,當在另一個運算符的內容中使用時,例如,o !is string中的運算子,它沒有任何作用,而且可以移除。

選項

此規則沒有相關聯的程式代碼樣式選項。

// Code with violations
if (o !is string) { }

// Potential fixes:
// 1.
if (o is not string) { }

// 2.
if (!(o is string)) { }

// 3.
if (o is string) { }

隱藏警告

如果您想要只隱藏單一違規,請將預處理器指示詞新增至原始程式檔以停用,然後重新啟用規則。

#pragma warning disable IDE0080
// The code that's violating the rule is on this line.
#pragma warning restore IDE0080

若要停用檔案、資料夾或項目的規則,請將其嚴重性設定為 組態檔中的 none

[*.{cs,vb}]
dotnet_diagnostic.IDE0080.severity = none

若要停用所有程式碼樣式規則,請將類別 Style 的嚴重性設定為 組態檔中的 none

[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none

如需詳細資訊,請參閱 如何在隱藏程式代碼分析警告。

另請參閱