this 和 Me 喜好設定 (IDE0003 和 IDE0009)

此文章描述兩個相關規則:IDE0003IDE0009

屬性
規則識別碼 IDE0003
標題 移除thisMe 限定性條件
類別 樣式
子類別 語言規則 ('this.' 和 'Me.' 限定詞)
適用語言 C# 和 Visual Basic
選項 dotnet_style_qualification_for_field
dotnet_style_qualification_for_property
dotnet_style_qualification_for_method
dotnet_style_qualification_for_event
屬性
規則識別碼 IDE0009
標題 新增thisMe 限定性條件
類別 樣式
子類別 語言規則 ('this.' 和 'Me.' 限定詞)
適用語言 C# 和 Visual Basic
選項 dotnet_style_qualification_for_field
dotnet_style_qualification_for_property
dotnet_style_qualification_for_method
dotnet_style_qualification_for_event

概觀

這兩個規則會定義您是否偏好使用 this (C#) Me. (Visual Basic) 限定詞。 若要強制實施限定詞「不」存在,將 IDE0003 的嚴重性設定為警告或錯誤。 若要強制實施限定詞「確實」存在,則將 IDE0009 的嚴重性設定為警告或錯誤。

例如,如果您偏好針對欄位和屬性 (但不針對方法或事件) 使用限定詞,則可啟用 IDE0009,並將選項 dotnet_style_qualification_for_fielddotnet_style_qualification_for_property 設定為 true。 不過,此組態不會標示「確實」具有 thisMe 限定詞的方法和事件。 如果也要強制實施方法與事件「沒有」限定詞,可啟用 IDE0003

選項

此規則的相關聯選項會定義此樣式喜好設定應套用至下列哪一個符號:

選項值 true 表示希望程式碼符號在 C# 中以 this. 開頭,而在 Visual Basic 中以 Me. 開頭。 選項值 false 表示希望「不要」在程式碼元素前面加上 this.Me.

如需設定選項的詳細資訊,請參閱選項格式

dotnet_style_qualification_for_field

屬性 描述
選項名稱 dotnet_style_qualification_for_field
選項值 true 希望欄位在 C# 中以 this. 開頭,或在 Visual Basic 中以 Me. 開頭
false 希望欄位「不要」以 this.Me. 開頭
預設選項值 false
// dotnet_style_qualification_for_field = true
this.capacity = 0;

// dotnet_style_qualification_for_field = false
capacity = 0;
' dotnet_style_qualification_for_field = true
Me.capacity = 0

' dotnet_style_qualification_for_field = false
capacity = 0

dotnet_style_qualification_for_property

屬性 描述
選項名稱 dotnet_style_qualification_for_property
選項值 true 希望屬性在 C# 中以 this. 開頭,或在 Visual Basic 中以 Me. 開頭。
false 希望屬性「不要」以 this.Me. 開頭。
預設選項值 false
// dotnet_style_qualification_for_property = true
this.ID = 0;

// dotnet_style_qualification_for_property = false
ID = 0;
' dotnet_style_qualification_for_property = true
Me.ID = 0

' dotnet_style_qualification_for_property = false
ID = 0

dotnet_style_qualification_for_method

屬性 描述
選項名稱 dotnet_style_qualification_for_method
選項值 true 希望方法在 C# 中以 this. 開頭,或在 Visual Basic 中以 Me. 開頭。
false 希望方法「不要」以 this.Me. 開頭。
預設選項值 false
// dotnet_style_qualification_for_method = true
this.Display();

// dotnet_style_qualification_for_method = false
Display();
' dotnet_style_qualification_for_method = true
Me.Display()

' dotnet_style_qualification_for_method = false
Display()

dotnet_style_qualification_for_event

屬性 描述
選項名稱 dotnet_style_qualification_for_event
選項值 true 希望事件在 C# 中以 this. 開頭,或在 Visual Basic 中以 Me. 開頭。
false 希望事件「不要」以 this.Me. 開頭。
預設選項值 false
// dotnet_style_qualification_for_event = true
this.Elapsed += Handler;

// dotnet_style_qualification_for_event = false
Elapsed += Handler;
' dotnet_style_qualification_for_event = true
AddHandler Me.Elapsed, AddressOf Handler

' dotnet_style_qualification_for_event = false
AddHandler Elapsed, AddressOf Handler

隱藏警告

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

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

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

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

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

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

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

另請參閱