OperationDelegate
using System;
namespace DelegateSample1
{
//define delegate
public delegate int OperationDelegate(int x, int y);
class Operator
{
private int _x, _y;
static void Main(string[] args)
{
OperationDelegate del = null;
del += new OperationDelegate(Plus);
del += new OperationDelegate(Subtract);
Operator op = new Operator(5, 3);
op.Operate(del);//Execute two method
//remove a delegate instance
del -= new OperationDelegate(Subtract);
op.Operate(del);
Console.ReadLine();
}
public void Operate(OperationDelegate del)
{
int i = del(_x, _y);
}
public Operator(int x, int y)
{
this._x = x;
this._y = y;
}
/// <summary>
/// Plus
/// </summary>
/// <param name="x">x</param>
/// <param name="y">y</param>
/// <returns></returns>
private static int Plus(int x, int y)
{
int result = x + y;
Console.WriteLine("x + y = {0}", result);
return result;
}
/// <summary>
/// Subtract
/// </summary>
/// <param name="x">x</param>
/// <param name="y">y</param>
/// <returns></returns>
private static int Subtract(int x, int y)
{
int result = x - y;
Console.WriteLine("x - y = {0}", result);
return result;
}
}
}
posted on 2011-11-15 22:40 breakpoint 阅读(117) 评论(0) 编辑 收藏 举报