this と Me の設定 (IDE0003 と IDE0009)

この記事では、IDE0003IDE0009 という 2 つの関連するルールについて説明します。

プロパティ
ルール ID IDE0003
タイトル this または Me の修飾を削除する
カテゴリ スタイル
Subcategory 言語ルール ('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
プロパティ
ルール ID IDE0009
タイトル this または Me の修飾を追加する
カテゴリ スタイル
Subcategory 言語ルール ('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

概要

これら 2 つの規則によって、this (C#) 修飾子と Me. (Visual Basic) 修飾子の使用を優先するかどうかを定義します。 修飾子が存在 "しない" ことを強制するには、IDE0003 の重要度を警告またはエラーに設定します。 修飾子が存在 "する" ことを強制するには、IDE0009 の重要度を警告またはエラーに設定します。

たとえば、フィールドとプロパティの修飾子は使用するが、メソッドやイベントの修飾子は使用しない場合は、IDE0009 を有効にして、オプション dotnet_style_qualification_for_fielddotnet_style_qualification_for_propertytrue に設定できます。 ただし、この構成では、this 修飾子と Me 修飾子が "ある" メソッドとイベントにフラグが設定されません。 メソッドとイベントにも修飾子が "ない" ことを強制するには、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

詳細については、「コード分析の警告を抑制する方法」を参照してください。

こちらもご覧ください