「C#」デリゲートに匿名メソッドを登録するサンプル
書式
delegate 戻り値の型 delegate名称(引数);
delegate名称 変数名 = delegate(引数){処理コード};
使用例
using System;
// デリゲートを定義 string型引数2つ
delegate void deleA(string strA, string strB);
class Arkgame
{
public static void Main()
{
//メソッド名がない匿名メソッド
deleA da = delegate(string strA, string strB)
{
Console.WriteLine(strA + strB + " become smart");
};
da("study", " skill ");
Console.ReadKey();
}
}
using System;
// デリゲートを定義 string型引数2つ
delegate void deleA(string strA, string strB);
class Arkgame
{
public static void Main()
{
//メソッド名がない匿名メソッド
deleA da = delegate(string strA, string strB)
{
Console.WriteLine(strA + strB + " become smart");
};
da("study", " skill ");
Console.ReadKey();
}
}
using System; // デリゲートを定義 string型引数2つ delegate void deleA(string strA, string strB); class Arkgame { public static void Main() { //メソッド名がない匿名メソッド deleA da = delegate(string strA, string strB) { Console.WriteLine(strA + strB + " become smart"); }; da("study", " skill "); Console.ReadKey(); } }
実行結果
study skill become smart