全体表示

[ リスト ]

【C#】Action / Func デリゲート

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
}

Tuple(タプル)

http://blogs.yahoo.co.jp/dk521123/23249027.html
に記載

閉じる コメント(0) ※投稿されたコメントはブログ開設者の承認後に公開されます。

コメント投稿
名前パスワードブログ
絵文字
×
  • SoftBank1
  • SoftBank2
  • SoftBank3
  • SoftBank4
  • docomo1
  • docomo2
  • au1
  • au2
  • au3
  • au4
投稿

閉じる トラックバック(0) ※トラックバックはブログ開設者の承認後に公開されます。

トラックバックされた記事

トラックバックされている記事がありません。

トラックバック先の記事

  • トラックバック先の記事がありません。
PR

.
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
 今日全体
訪問者685253751
ブログリンク00
コメント021
トラックバック03

友だち(2人)
Yahoo Image

人気度

ヘルプ

Yahoo Image

Yahoo!スマホマネージャー
PR

PR


プライバシーポリシー -  利用規約 -  ガイドライン -  順守事項 -  ヘルプ・お問い合わせ

Copyright (C) 2013 Yahoo Japan Corporation. All Rights Reserved.

みんなの更新記事