by Emil
8. July 2009 15:42
委托是一种引用方法的类型。一旦为委托分配了方法,委托将与该方法具有完全相同的行为。
委托方法的使用可以像其他任何方法一样,具有参数和返回值。委托就是给一个函数取了个别的名字。
using System;
// Declare delegate -- defines required signature:
delegate void SampleDelegate(string message);
class MainClass
{
// Regular method that matches signature:
static void SampleDelegateMethod(string message)
{
Console.WriteLine(message);
}
static void Main()
{
// Instantiate delegate with named method:
SampleDelegate d1 = SampleDelegateMethod;
// Instantiate delegate with anonymous method:
SampleDelegate d2 = delegate(string message)
{
Console.WriteLine(message);
};
d1("Hello");
d2("World,http://www.dc9.cn/");
}
}
下面也是委托:
public Form1()
{
InitializeComponent();
this.Click += this.MultiHandler;
}
private void MultiHandler(object sender, System.EventArgs e)
{
MessageBox.Show(((MouseEventArgs)e).Location.ToString());
}
用lambda写就是:
public Form1()
{
InitializeComponent();
this.Click += (sender, e) => {MessageBox.Show(((MouseEventArgs)e).Location.ToString());};
}
--------------------------------------------------------------------------------------------------------------------------------------------
delegate string Printer(string s);
private void button1_Click(object sender, EventArgs e)
{
Printer p =delegate(string j)
{
return j;
};
Console.WriteLine(p("The delegate using the anonymous method is called."));
}
用lambda写就是:
delegate string Printer(string s);
private void button1_Click(object sender, EventArgs e)
{
Printer p = j => j;
Console.WriteLine(p("The delegate using the anonymous method is called."));
}
--------------------------------------------------------------------------------------------------------------------------------------------
或者有名的匿名委托:
delegate void Printer(string s);
private void button1_Click(object sender, EventArgs e)
{
Printer p = new Printer(Form1.DoWork);
p("http://www.dc9.cn/");
}
static void DoWork(string k)
{
System.Console.WriteLine(k);
}
--------------------------------------------------------------------------------------------------------------------------------------------
Where是Enumerable的一个方法。里面的参数是Func<(Of <(T, TResult>)>) 泛型委托
using System;
delegate string ConvertMethod(string inString);
public class DelegateExample
{
public static void Main()
{
// Instantiate delegate to reference UppercaseString method
ConvertMethod convertMeth = UppercaseString;
string name = "Dakota";
// Use delegate instance to call UppercaseString method
Console.WriteLine(convertMeth(name));
}
private static string UppercaseString(string inputString)
{
return inputString.ToUpper();
}
}
写成泛型委托是:
using System;
public class GenericFunc
{
public static void Main()
{
// Instantiate delegate to reference UppercaseString method
Func<string, string> convertMethod = UppercaseString;
string name = "Dakota";
// Use delegate instance to call UppercaseString method
Console.WriteLine(convertMethod(name));
}
private static string UppercaseString(string inputString)
{
return inputString.ToUpper();
}
}
-------------------------------------------------------------------------------------------------------------------------------------------
于是应用到Linq,再变换到lambda:
delegate bool TestFunc(string fruit);
private void button1_Click(object sender, EventArgs e)
{
List<string> fruits =new List<string> {"apple", "http://www.dc9.cn", "banana",
"mango", "orange", "blueberry", "grape", "strawberry" };
TestFunc f = new TestFunc(DoWork);
Func<string, bool> f2 = DoWork;
IEnumerable<string> query = fruits.Where(f2);
foreach (string fruit in query)
Console.WriteLine(fruit);
}
private static bool DoWork(string k)
{
return k.Length < 6;
}
换成lambda写就是:
delegate bool TestFunc(string fruit);
private void button1_Click(object sender, EventArgs e)
{
List<string> fruits =new List<string> { "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "http://www.dc9.cn" };
TestFunc f = DoWork;
Func<string, bool> f2 =k=> k.Length < 6;
IEnumerable<string> query = fruits.Where(f2);
foreach (string fruit in query)
Console.WriteLine(fruit);
}
private static bool DoWork(string k)
{
return k.Length < 6;
}
-------------------------------------------------------------------------------------------------------------------------------------------
也能用
private void button1_Click(object sender, EventArgs e)
{
List<string> fruits =new List<string> { "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "http://www.dc9.cn" };
IEnumerable<string> query = fruits.Where(
delegate(string k){
return k.Length < 6;
}
);
foreach (string fruit in query)
Console.WriteLine(fruit);
}
换成lambda写就是:
private void button1_Click(object sender, EventArgs e)
{
List<string> fruits =new List<string> { "apple", "passionfruit", "banana","mango",
"orange", "blueberry", "grape", "http://www.dc9.cn" };
IEnumerable<string> query = fruits.Where(k=>k.Length<6);
foreach (string fruit in query)
Console.WriteLine(fruit);
}
--------------------------------------------------------------------------------------------------------------------------------------------
最后再写一个:
public delegate int mydg(int a, int b);
public static class LambdaTest
{
public static int oper(this int a, int b, mydg dg)
{
return dg(a, b);
}
}
Console.WriteLine(oper(2,3, (a, b) => a + b));
Console.WriteLine(oper(2,3, (a, b) => a - b));
ebfd9087-1e01-47bb-9e52-7a04974c14cd|0|.0
Tags:
Email2Blog