「C#」デリゲートに匿名メソッドを登録するサンプル

書式
delegate 戻り値の型 名称 (引数)
登録するメソッドは、delegateの引数と戻り値の型に一致している必要があります。
デリゲートにメソッドを=で代入した場合は、メソッドの上書きになります。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
delegate void delegateA(int num1, int num2);
class Demo
{
public static void Main()
{
delegateA cft = delegate(int a,int b){ Console.WriteLine(a * b);};
cft(4, 5);
}
}
using System; delegate void delegateA(int num1, int num2); class Demo { public static void Main() { delegateA cft = delegate(int a,int b){ Console.WriteLine(a * b);}; cft(4, 5); } }
using System;

delegate void delegateA(int num1, int num2);

class Demo
{
      public static void Main()
      {
            delegateA cft = delegate(int a,int b){ Console.WriteLine(a * b);};
            cft(4, 5);
      }
}

実行結果
20

C#

Posted by arkgame