將匿名型別轉換為元組 (IDE0050)

重要

此樣式規則已在 Visual Studio 2022 中移除,並轉換成 Visual Studio 重構。 如需重構的相關資訊,請參閱將匿名型別轉換為元組

屬性
規則識別碼 IDE0050
標題 將匿名型別轉換為元組
類別 樣式
子類別 語言規則 (運算式層級喜好設定)
適用語言 C# 和 Visual Basic

概觀

匿名型別有兩個以上的欄位時,此規則建議對匿名型別使用元組

選項

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

範例

// Code with violations
var t1 = new { a = 1, b = 2 };

// Fixed code
var t1 = (a: 1, b: 2);
' Code with violations
Dim t1 = New With { .a = 1, .b = 2 }

' Fixed code
Dim t1 = (a:=1, b:=2)

隱藏警告

若您只想隱藏單一違規,請將前置處理指示詞新增至來源檔案以停用規則,然後重新啟用規則。

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

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

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

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

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

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

另請參閱