标签:
public delegate void maojiaodelegate (object Sender, EventArgs e); /// <summary> /// 触发对象 观察者 人醒来 /// </summary> public class BigMaster { private Mao _cat; public BigMaster(Mao cat) { _cat = cat; _cat.onjiao += new maojiaodelegate(onxinglaile); } public void onxinglaile(object Sender, EventArgs e) { xinglaile(); } public void xinglaile() { Console.WriteLine("xinglai"); } } /// <summary> /// 事件引发者 被观察者 猫叫 /// </summary> public class Mao { public event maojiaodelegate onjiao; public void catjiao( EventArgs e) { if (onjiao != null) { onjiao(this, e); } } public void catjiao() { // Console.WriteLine("jiao"); EventArgs e=new EventArgs(); catjiao(e); } } /// <summary> /// 触发对象 观察者 老鼠跑了 /// </summary> public class Lashu { private Mao _cat; public Lashu(Mao cat) { _cat = cat; _cat.onjiao += new maojiaodelegate(onrunningla); } public void onrunningla(object Sender,EventArgs e) { runningla(); } public void runningla() { Console.WriteLine("pao"); } }
Mao mao = new Mao(); mao.catjiao(); BigMaster p = new BigMaster(mao); Lashu lao = new Lashu(mao);
Demo 委托 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 using System; using System.Collections.Generic; using System.Text; namespace DelegateDemo { class Class1 { delegate double processDelegate(double db1, double db2); static double Multiply(double db1, double db2) { return db1 * db2; } static double Divide(double db1, double db2) { return db1 / db2; } static void NamedMethod(string strInput,double dbNum1,double dbNum2) { processDelegate process; if (strInput == "M") process = new processDelegate(Multiply); else process = new processDelegate(Divide); Console.WriteLine("结果为:{0}", process(dbNum1, dbNum2)); }
//匿名的方法 static void AnonymousMethod(string strInput, double dbNum1, double dbNum2) { processDelegate process; if (strInput == "M") process = delegate(double db1, double db2){return db1 * db2;}; else process = delegate(double db1, double db2) { return db1 / db2; }; Console.WriteLine("结果为:{0}", process(dbNum1, dbNum2)); } /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main(string[] args) { // // TODO: 在此处添加代码以启动应用程序 Console.WriteLine("请输入两个小数,用逗号分割"); string strInput = Console.ReadLine(); int commmaPos = strInput.IndexOf(‘,‘); double dbNum1 = Convert.ToDouble(strInput.Substring(0, commmaPos)); double dbNum2 = Convert.ToDouble(strInput.Substring(commmaPos + 1)); Console.WriteLine("输入M表示乘法,或者D表示除法"); strInput = (Console.ReadLine()).ToUpper(); //使用命名方法 Console.WriteLine("使用命名方法委托"); NamedMethod(strInput,dbNum1,dbNum2); //使用匿名方法 Console.WriteLine("使用匿名方法委托"); AnonymousMethod(strInput, dbNum1, dbNum2); } } }
委托实例:http://www.cnblogs.com/liulun/archive/2009/10/25/1589523.html
http://www.cnblogs.com/liulun/archive/2009/05/29/1491912.html
事件多播:http://www.cnblogs.com/liulun/archive/2009/09/24/1573571.html
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace xiancheng { class Program { delegate void mytest(int a,int b); static void Main(string[] args) { mytest tee = delegate(int x,int y) { int result = x + y; Console.WriteLine("这是匿名委托用法结果是: "+result); }; tee(2, 8); mytest tee2 = new mytest(getresult); tee2(9,8); //拉姆达 mytest tee3 = (x,y)=> { int result = x + y; Console.WriteLine("这是拉姆达匿名委托用法结果是: " + result); }; tee3(88, 12); Console.ReadKey(); } public static void getresult(int x, int y) { int result = x + y; Console.WriteLine("这不是匿名委托方法使用结果是: "+result) ; } } }
输出 10,17,100
9.Lambda表达式
(1)源起
.net的设计者发现在使用匿名方法时,
仍旧有一些多余的字母或单词的编码工作
比如delegate关键字
于是进一步简化了匿名方法的写法
(2)使用
List<int> arr = new List<int>() { 1, 2, 3, 4, 5, 6, 7 }; arr.ForEach(new Action<int>(delegate(int a) { Console.WriteLine(a); })); arr.ForEach(new Action<int>(a => Console.WriteLine(a)));
匿名方法的代码如下:
delegate(int a) { Console.WriteLine(a); }
使用lambda表达式的代码如下:
a => Console.WriteLine(a)
这里解释一下这个lambda表达式
<1>
a是输入参数,编译器可以自动推断出它是什么类型的,
如果没有输入参数,可以写成这样:
() => Console.WriteLine("ddd")
<2>
=>是lambda操作符
<3>
Console.WriteLine(a)是要执行的语句。
如果是多条语句的话,可以用{}包起来。
如果需要返回值的话,可以直接写return语句
标签:
原文地址:http://www.cnblogs.com/annabook/p/4583357.html