class deleMethod
{
public int add(int x, int y)
{
return x + y;
}
public int minus(int x, int y)
{
return x - y;
}
public int multi(int x, int y)
{
return x * y;
}
}
class Program
{
public delegate int Handler(int x, int y); //---定义委托的类型,可以将委托看成一种特殊的数据类型
static void Main(string[] args)
{
deleMethod dm = new deleMethod(); //---实例化包含委托方法的类型
Handler deleCall = new Handler(dm.add); //---定义委托变量delCall,并出示化赋值
int result1 = deleCall(10, 20); //---实例方法的调用invoke
Console.WriteLine("the add resutl is:{0}", result1);
deleCall = dm.minus;
int result2 = deleCall.Invoke(12, 6);
Console.WriteLine("the minus result is:{0}", result2);
Console.ReadLine();
}
}
namespace DelegateDemo1
{
class Program
{
static void Main(string[] args)
{
deleMethod dm = new deleMethod();
Func<int, int, int> fun = dm.add; //---使用预定义的委托类型Func<>
int result4 = fun(8, 10);
Func<int, int, int> fun1 = dm.minus;
int result5 = fun1(12, 8);
Console.WriteLine("预定义的委托输出{0},{1}", result4, result5);
Console.ReadLine();
}
}
class deleMethod
{
public int add(int x, int y)
{
return x + y;
}
public int minus(int x, int y)
{
return x - y;
}
public int multi(int x, int y)
{
return x * y;
}
}
}
namespace DelegateDemo1
{
class Program
{
static void Main(string[] args)
{
Func<int, int, int> fun = delegate(int x, int y) { return x + y; };
Func<int, int, int> fun1 = delegate(int x, int y) { return x - y; };
int result4 = fun(8, 10);
int result5 = fun1(12, 8);
Console.WriteLine("预定义的委托输出{0},{1}", result4, result5);
Console.ReadLine();
}
}
}
namespace DelegateDemo1
{
class Program
{
static void Main(string[] args)
{
Func<int, int, int> fun = (x, y) => x + y;
Func<int, int, int> fun1 = (x, y) => x - y;
int result4 = fun(8, 10);
int result5 = fun1(12, 8);
Console.WriteLine("预定义的委托输出{0},{1}", result4, result5);
Console.ReadLine();
}
}
}
class Program
{
static void Main(string[] args)
{
//Action 表示没有返回值的一类方法
Action<int, int> actionA = (x, y) =>
{
Console.WriteLine("x是{0},y是{1},他们的平方和是{2}", x, y, x * x + y * y);
};
actionA += (x, y) =>
{
Console.WriteLine("x是{0},y是{1},他们的平方差是{2}", x, y, x * x - y * y);
};
actionA(10, 5);
foreach (var item in actionA.GetInvocationList())
Console.WriteLine(item.Method);
Console.ReadLine();
}
}
/// <summary>
/// 主程序类
/// </summary>
class Program
{
static void Main(string[] args)
{
eventPublish myEvent = new eventPublish();
eventSubscription myMethod = new eventSubscription();
//绑定方法 add 和 subtract,又称为对事件方法的注册 -=称为对事件方法的注销
myEvent.calc += myMethod.add;
myEvent.calc += myMethod.substract;
while (true)
{
try
{
Console.WriteLine("请输入第一个整数数");
int numA = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("请输入第二个整数");
int numB = Convert.ToInt16(Console.ReadLine());
calcEventArgs e = new calcEventArgs(numA, numB);
//在本例不需要sender参数,随意传递了字符串"sender"
myEvent.onCalc("sender", e);
}
catch (Exception ex)
{
Console.WriteLine("出现错误," + ex.Message);
}
}
}
}
/// <summary>
/// 定义一个事件发布器类
/// </summary>
class eventPublish
{
//定义一个委托类型,委托名一般是 事件变量名+EventHandler
public delegate void calcEventHander(object sender,calcEventArgs e);
//定义一个事件变量,其变量名为calc
public event calcEventHander calc;
//封装事件,对外定义了引发事件的方法,定义的引发方法名一般是 on+事件变量名
public void onCalc(object sender, calcEventArgs e)
{
if (calc != null)
calc(sender,e);
}
}
/// <summary>
/// 定义一个事件订阅者类(事件方法类)
/// </summary>
class eventSubscription
{
public void add(object sender, calcEventArgs e)
{
Console.WriteLine("两数相加等于{0}", e.X + e.Y );
}
public void substract(object sender, calcEventArgs e)
{
Console.WriteLine("两数相减等于{0}", e.X - e.Y);
}
}
/// <summary>
/// 定义一个事件参数类
/// </summary>
class calcEventArgs : EventArgs
{
private int _x;
private int _y;
public calcEventArgs(int x, int y)
{
this._x = x;
this._y = y;
}
public int X
{
get { return _x; }
}
public int Y
{
get { return _y; }
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有