【C#】Delegate / Event ~ Action デリゲート ~

■ はじめに

https://dk521123.hatenablog.com/entry/2010/12/12/164101
https://dk521123.hatenablog.com/entry/2010/12/25/221009
https://dk521123.hatenablog.com/entry/2010/10/22/101350

の続き。

 ■ Action デリゲート

 Actinの意味

・T型の引数を1つ、戻り値なし(void)メソッドをとるデリゲート

 構文

private Action<【引数の型】, ...> 【メソッドを格納する変数】 { get; set; }

【メソッドを格納する変数】 = 【メソッド名】

 ■ サンプル

private string Name { get; set; }
private string Occupation { get; set; }
private Action<string> Method { get; set; }

private void button1_Click(object sender, EventArgs e)
{
    this.Name = "Blank";
    this.Occupation = "Blank";

    // this.Method = new Action<string>(this.SetOccupation);と同じ
    this.Method = this.SetOccupation;
    this.Method("Programmer");

    this.label1.Text = this.Name;
    this.label2.Text = this.Occupation;
}

private void SetName(string value)
{
    this.Name = value;
}

private void SetOccupation(string value)
{
    this.Occupation = value;
}

 出力結果

Blank
Programmer

 参考文献

http://itnandemolab.blog70.fc2.com/blog-entry-719.html

 関連記事

Delegate / Event ~ 入門編 / Delegate
https://dk521123.hatenablog.com/entry/2010/12/12/164101
Delegate / Event ~ 入門編 / Event ~
https://dk521123.hatenablog.com/entry/2010/12/25/221009