class StringTest3
{
public class MethodClass
{
public void Method1(int x, int y)
{
Console.WriteLine("{0} + {1} = {2}", x, y, x + y);
}
public void Method2(int x, int y)
{
Console.WriteLine("{0} - {1} = {2}", x, y, x - y);
}
public void Method3(int x, int y)
{
Console.WriteLine("{0} * {1} = {2}", x, y, x * y);
}
public void Method4(int x, int y)
{
Console.WriteLine("{0} / {1} = {2}", x, y, x / y);
}
}
delegate void Del(int x,int y);
public StringTest3()
{
MethodClass obj = new MethodClass();
Del d1 = obj.Method1;
Del d2 = obj.Method2;
Del d3 = obj.Method3;
Del d4 = obj.Method4;
Del allMethod = d1 + d2;
allMethod += d3 + d4;
allMethod -= d1;
MethodWithCallBack(48, 24, allMethod);
}
private void MethodWithCallBack(int x, int y, Del allMethod)
{
Console.WriteLine("現在のデリゲートリストの数=" + allMethod.GetInvocationList().GetLength(0));
allMethod(x,y);
}
}
色々なデリゲートインスタンスの割り当て方
class TestTest2
{
delegate void Del(int x,int y);
void Method(int x, int y)
{
Console.WriteLine(x-y);
}
public TestTest2()
{
Del call;
Del d1 = Method;
Del d2 = delegate(int x, int y) { Console.WriteLine(x * y); };
Del d3 = (x, y) => { Console.WriteLine(x + y); };
call = d1 + d2 + d3;
call(12, 13);
}
}
最終更新:2012年06月05日 17:06