LoginSignup
0
0

More than 5 years have passed since last update.

Visual Studio / WPF | CSharp > public static string DisplayName(this Gender gender)という書き方 > 拡張メソッド

Last updated at Posted at 2017-06-14
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community

WPF データーバインディングを用いた表示について
http://qiita.com/hugo-sb/items/be3a8148d1397d7a6545
を元に学習をしていて、以下の文法が分からなかった。

public static string DisplayName(this Gender gender)

第一引数のthisは何を意味するのか。
(追記: コメントに「// Genderへの拡張メソッドの定義」とありました)。

++C++; // 未確認飛行 Cさん
TOP C# によるプログラミング入門 [関数指向] 拡張メソッド
によると拡張メソッドの書き方のようだ。

拡張メソッドは、静的メソッドをインスタンスメソッドと同じ形式で呼び出せるようにできるものです。

元記事では以下のようにインスタントメソッドと同じ形式で使用している。

public string Gender
{
    get { return Person.Gender.DisplayName(); }
}

拡張メソッドを使う利点や注意点についてはリンク2つ目(++C++; // 未確認飛行 Cさん)に詳しい。

関連記事

[C#]ちょっとしたストレスを解消してくれるExtensions

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0