「C#」匿名メソッドを使用するサンプル
書式
1.匿名メソッドの定義
delegate void メソット名(データ型 変数名);
2.匿名メソッドの使用
メソッド名 変数名= (データ型 変数名) => {処理コード}
使用例
using System;
//匿名メソッド funAの宣言
delegate void funA(string str);
//匿名メソッド funBの宣言
delegate void funB(int age);
class ArkgameStudyDemo
{
public static void Main()
{
//匿名funAを使用
funA cft = (string str) => { Console.WriteLine(str + " in arkgame"); };
cft("study skill");
//匿名funBを使用
funB cftB = (int age) => { Console.WriteLine(age*2); };
cftB(12);
Console.ReadKey();
}
}
using System;
//匿名メソッド funAの宣言
delegate void funA(string str);
//匿名メソッド funBの宣言
delegate void funB(int age);
class ArkgameStudyDemo
{
public static void Main()
{
//匿名funAを使用
funA cft = (string str) => { Console.WriteLine(str + " in arkgame"); };
cft("study skill");
//匿名funBを使用
funB cftB = (int age) => { Console.WriteLine(age*2); };
cftB(12);
Console.ReadKey();
}
}
using System; //匿名メソッド funAの宣言 delegate void funA(string str); //匿名メソッド funBの宣言 delegate void funB(int age); class ArkgameStudyDemo { public static void Main() { //匿名funAを使用 funA cft = (string str) => { Console.WriteLine(str + " in arkgame"); }; cft("study skill"); //匿名funBを使用 funB cftB = (int age) => { Console.WriteLine(age*2); }; cftB(12); Console.ReadKey(); } }
実行結果
study skill in arkgame
24