namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var myDele = new TestDelegate();
myDele.Execute();
Console.ReadLine();
}
}
delegate int myDelegate(int x, int y);
class TestDelegate
{
public void Execute()
{
myDelegate AddDele = Add;
myDelegate SubDele = Sub;
MessageShow(2, 2, AddDele);
MessageShow(2, 2, SubDele);
}
public void MessageShow(int x,int y,myDelegate myDeli)
{
Console.WriteLine(myDeli(x, y));
}
public int Add(int x,int y)
{
return x + y;
}
public int Sub(int x, int y)
{
return x - y;
}
}
}
▸ 実行ボタンで結果を表示
Source収録記事
この snippet は記事の「デリゲート型を宣言していくパターン」セクションに登場する。コードの前後の文脈・ハマりどころの解説は記事本文で。
