Delegate Demo 匿名委托 Lambda


using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace ObjDelegate { delegate void ObjExtDe(string name); delegate int ObjExtDe<T>(T t1, T t2); public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ObjExtDe de = new ObjExtDe(ObjExt);//委托就是方法的容器(既然是容器申明的时候就必须要有方法) de += ObjJPxt; de -= ObjExt; de("name");//调用委托 ObjJPxt_("tsp", ObjJPxt);//把方法当做参数传递 } void ObjExt(string name) { MessageBox.Show("你好 "+name); } void ObjJPxt(string name) { MessageBox.Show("hello "+name); } void ObjJPxt_(string name,ObjExtDe De) { De(name);//这个写法是不是很奇特呢?de就是方法他有个参数 name 你在看看委托的申明是不是有个参数(方法的容器我也理解是模版) } private void button2_Click(object sender, EventArgs e) { int[] myArr = { 1, 3, 45, 66 }; MessageBox.Show("数组普通方法 "+GetMax(myArr).ToString()); MessageBox.Show("数组委托方法 " + GetMax<int>(myArr, GetMax).ToString()); MessageBox.Show("数组匿名委托 " + GetMax(myArr, delegate(int x, int y) { return x > y ? 1 : 0; }).ToString()); MessageBox.Show("数组Labma方法 " + GetMax(myArr, (x, y) => x > y ? 1 : 0)); Penson[] ps = new Penson[] { new Penson{Name="sp",Age=1}, new Penson{Name="sp",Age=3}, new Penson{Name="sp",Age=4} }; var sp = new[] { new { Name = "sp", Age = 1 }, new { Name = "sp", Age = 3 }, new { Name = "sp", Age = 4 } };//匿名数组初始化前天的时候没有写出了 今天正好写对了 呵呵 MessageBox.Show("Penson普通方法 " + GetMax(ps).Age); MessageBox.Show("Penson委托方法 " + GetMax<Penson>(ps, GetMax).Age); MessageBox.Show("Penson匿名方法 " + GetMax(ps, delegate(Penson x, Penson y) { return x.Age > y.Age ? 1 : 0; }).Age); MessageBox.Show("PensonLabma方法 " + GetMax(ps, (Penson x, Penson y) => x.Age > y.Age ? 1 : 0).Age); } int GetMax(int[] arr) { int max = arr[0]; for (int i = 1; i < arr.Length; i++) { if (arr[i]>max) { max = arr[i]; } } return max; } Penson GetMax(Penson[] arr) { Penson max = arr[0]; for (int i = 1; i < arr.Length; i++) { if (arr[i].Age>max.Age) { max = arr[i]; } } return max; } T GetMax<T>(T[] arr,ObjExtDe<T> sp) { T max = arr[0]; for (int i = 1; i < arr.Length; i++) { if (sp(arr[i],max)==1) //sp(arr[i],max)是委托方法的 开始在写这个总是出错 { max = arr[i]; } } return max; } int GetMax(int x,int y) { return x > y ? 1 : 0; } int GetMax(Penson arr1, Penson arr2) { return arr1.Age > arr2.Age ? 1 : 0; } /// <summary> /// Penson类 /// </summary> public sealed class Penson { private string name; public string Name { get { return name; } set { name = value; } } private int age; public int Age { get { return age; } set { age = value; } } } } }

  -- 解析(x,y)=>x>y?1:0

// 如果看到有个这个的
// tsp.Where((x,y)=>x?1:0);

//第一肯定有一个类 类里面有个静态方法一个方法(这个是必须的)

public clss tsp
{
public static int Where(int x,int y)
{
return x>y?1:0;
}
}

改进
//第一肯定有一个类 类里面有个静态方法一个方法(这个也应该想到)

public clss tsp
{
public static int Where(myDelegate);//myDelegate是委托(就是方法的容器)
}

接着:
肯定有个委托申明
public delegate int myDelegate(int x,int y);

下面的就是申明和调用
myDelegate spDelegate=new myDelegate(xxxxx);
tsp sp1=new sp1();
sp1.Where(spDelegate);

posted @ 2012-10-20 18:23  s_p  阅读(193)  评论(0编辑  收藏  举报