委托的几个实例用法
class Program
{
private delegate void deleFunc(string str);
static void Main(string[] args)
{
deleFunc func = new Program().Chinese;
//func("张三");
func += English;
func("alex");
Console.WriteLine( "==================");
//匿名委托
deleFunc func1 = delegate(string name)
{
Console.WriteLine("你好,{0}", name);
};
func1("李四");
Console.WriteLine("==================");
//lambda
deleFunc func2 = (name => { Console.WriteLine("Hello, {0}", name); });
func2("Jack");
//action
Console.WriteLine("==================");
Action<string> acFun = (m => { Console.WriteLine("您好,{0}", m); });
acFun("中国人");
Func<string, string> funcDelegate = (m => { return string.Format("Hello,{0}", m); });
string result = funcDelegate("chinese man");
Console.WriteLine(result);
//实例应用
int[] i = { 1, 8, 45, 15, 68, 12, 66 };
Func<int, bool> foo = temp_i => temp_i > 10;
var list = i.Where(foo);
foreach (int i1 in list)
{
Console.WriteLine(i1);
}
Console.WriteLine("==================");
i.Where(m => m > 15).ToList().ForEach(
s => { Console.WriteLine(s); }
);
Console.Read();
}
private void Chinese(string name)
{
Console.WriteLine("你好:{0}",name);
}
private static void English(string name)
{
Console.WriteLine("Hello, {0}",name);
}
}
{
private delegate void deleFunc(string str);
static void Main(string[] args)
{
deleFunc func = new Program().Chinese;
//func("张三");
func += English;
func("alex");
Console.WriteLine( "==================");
//匿名委托
deleFunc func1 = delegate(string name)
{
Console.WriteLine("你好,{0}", name);
};
func1("李四");
Console.WriteLine("==================");
//lambda
deleFunc func2 = (name => { Console.WriteLine("Hello, {0}", name); });
func2("Jack");
//action
Console.WriteLine("==================");
Action<string> acFun = (m => { Console.WriteLine("您好,{0}", m); });
acFun("中国人");
Func<string, string> funcDelegate = (m => { return string.Format("Hello,{0}", m); });
string result = funcDelegate("chinese man");
Console.WriteLine(result);
//实例应用
int[] i = { 1, 8, 45, 15, 68, 12, 66 };
Func<int, bool> foo = temp_i => temp_i > 10;
var list = i.Where(foo);
foreach (int i1 in list)
{
Console.WriteLine(i1);
}
Console.WriteLine("==================");
i.Where(m => m > 15).ToList().ForEach(
s => { Console.WriteLine(s); }
);
Console.Read();
}
private void Chinese(string name)
{
Console.WriteLine("你好:{0}",name);
}
private static void English(string name)
{
Console.WriteLine("Hello, {0}",name);
}
}