Action デリゲート
Actin<T>の意味
・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.GetOccupation);と同じ
this.Method = this.GetOccupation;
this.Method("Programmer");
this.label1.Text = this.Name;
this.label2.Text = this.Occupation;
}
private void GetName(string value)
{
this.Name = value;
}
private void GetOccupation(string value)
{
this.Occupation = value;
}
出力結果
Blank
Programmer
参考文献
http://itnandemolab.blog70.fc2.com/blog-entry-719.html
Func デリゲート
Func<TResult>
Func<T, TResult>
Func<T1, T2,... TResult>
構文
Func<(【引数の型】,)【戻り値の型】> 【メソッド名】 = (【引数】) => 【式】;
サンプル
private void button2_Click(object sender, EventArgs e)
{
Func<int> f1 = () => 1;
this.label1.Text = f1().ToString(); // 出力結果:1
Func<int, int> f2 = (x) => x * 2;
this.label2.Text = f2(2).ToString(); // 出力結果:4
Func<int, int, int> f3 = (x, y) => x * y;
this.label3.Text = f3(3, 4).ToString(); // 出力結果:12
}
|