学关于Lambda表达式的一二三
给自己写篇自己能看得懂的文章,希望以后再白痴的时候看看就不白痴了。
委托是一种引用方法的类型。一旦为委托分配了方法,委托将与该方法具有完全相同的行为。委托方法的使用可以像其他任何方法一样,具有参数和返回值。
委托就是给一个函数起一个别的名字。
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());
}
匿名方法
匿名方法就是没名儿的委托。虽然没名,但是必须加”delegate“来表示我没名。
// Create a delegate instance
delegate void Del(int x);
// Instantiate the delegate using an anonymous method
Del d = delegate(int k) { /* ... */ };
Del d = delegate() { System.Console.WriteLine("Copy #:{0}", ++n); };
delegate void Printer(string s);
Printer p = delegate(string j)
{
System.Console.WriteLine(j);
};
p("The delegate using the anonymous method is called.");
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."));
}
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);
}
Lamdba 就是 (int x)=>{x+1}就是这样的。。。例子呢就是上面写了的一段用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."));
}
和上面那个一样,就是简化了。
public Form1()
{
InitializeComponent();
this.Click += (s, e) => { MessageBox.Show(((MouseEventArgs)e).Location.ToString());};
}
超难+===>?其实不难。难的是转换成一般的函数怎么写呢????
。。。。。
Where是Enumerable的一个方法。3.5才有的。里面的参数是Func<(Of <(T, TResult>)>) 泛型委托
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;
}
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);
}
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(1.oper(2, (a, b) => a + b));
Console.WriteLine(2.oper(1, (a, b) => a - b));