C# 委托(二) Delegate、Action、Func
一、Delegate委托:
一般带返回值的Delegate委托:
using System;
namespace Delegate
{
public delegate int D(int a,int b);//声明委托类型
class Program
{
static void Main(string[] args)
{
int a = 2;//声明整型变量
int b = 3;//声明整型变量
D d = new D(F);//声明委托变量 并初始化
Console.WriteLine("{0}+{1}={2}",a,b,d(a,b));//输出
Console.ReadKey();//按任意键
}
static int F(int x,int y)//静态方法
{
return x + y;//返回
}
}
}输出结果:
使用lambda表达式:
using System;
namespace Delegate
{
public delegate int D(int a);//声明委托类型
class Program
{
static void Main(string[] args)
{
int x = 10;//声明整型变量
Console.WriteLine("—使用完整匿名方法—");
D d1 = delegate (int a) { return a + 1; };//使用完整匿名方法
Console.WriteLine(d1(x));//输出
Console.WriteLine("—使用Lambda表达式1—");
D d2 = (int a) => { return a + 1; };//使用Lambda表达式1
Console.WriteLine(d2(x));//输出
Console.WriteLine("—使用Lambda表达式2—");
D d3 = (a) => { return a + 1; };//使用Lambda表达式2 可以省略参数类型。
Console.WriteLine(d3(x));//输出
Console.WriteLine("—使用Lambda表达式3—");
D d4 = a => { return a + 1; };//使用Lambda表达式3 只有一个隐式类型参数,可以省略小括号。
Console.WriteLine(d4(x));//输出
Console.WriteLine("—使用Lambda表达式4—");
D d5 = a => a + 1;//使用Lambda表达式4 如果语句块只有一个返回语句,可以省略 return 和大括号。
Console.WriteLine(d5(x));//输出
Console.ReadKey();//按任意键
}
}
}输出语句:
二、Action委托:可以传人参数,没有返回值
调用方法:
using System;
namespace Action
{
class Program
{
static void Main(string[] args)
{
Action<int, int> A = new Action<int, int>(Add);//实例化一个委托
A(2,3);//调用委托
Console.ReadKey();//按任意键
}
static void Add(int a,int b)//静态方法,无返回值
{
Console.WriteLine("{0}+{1}={2}",a,b,a + b);//输出
}
}
}输出结果:
使用lambda表达式:
using System;
namespace Action
{
class Program
{
static void Main(string[] args)
{
Action<int, int> A = (a, b) => Console.WriteLine("{0}+{1}={2}", a, b, a + b);//实例化一个委托
A(2,3);//调用委托
Console.ReadKey();//按任意键
}
}
}输出结果:
作为参数传:
using System;
namespace Action
{
class Program
{
static void Main(string[] args)
{
Action<string> A1 = p => Console.WriteLine("A1传的值为:{0}",p);//实例化一个委托
Action<string> A2 = p => Console.WriteLine("A2传的值为:{0}", p);//实例化一个委托
F(A1,"小道博客");//调用F方法,传入委托参数
F(A2,"http://www.daobk.com");//调用F方法,传入委托参数
Console.ReadKey();//按任意键
}
static void F<T>(Action<T> a,T input)//方法
{
a(input);//调用委托
}
}
}输出结果:
三、Func委托:可以传入参数,必须有返回值。
调用方法:
using System;
namespace Func
{
class Program
{
static void Main(string[] args)
{
Func<string> F = new Func<string>(S);//实例化一个委托
string s = F();//将返回值赋值给变量s
Console.WriteLine(s);//输出s的值
Console.ReadKey();//按任意键
}
static string S()//静态方法
{
return "小道博客:http://www.daobk.com";//返回值
}
}
}输出结果:
使用Lambda表达式:
using System;
namespace Func
{
class Program
{
static void Main(string[] args)
{
Func<string> F1 = () => "小道博客";//实例化一个委托,不加大括号,直接写返回值,不能加 return
Func<string> F2 = () =>
{
string a = "http://";
a += "www.daobk.com";
return a;
};//另一种实例化一个委托,加大括号,可以写多行代码,但必须加上 return
string s1 = F1();//调用委托
string s2 = F2();//调用委托
Console.WriteLine(s1);//输出
Console.WriteLine(s2);//输出
Console.ReadKey();//按任意键
}
}
}输出结果:
作为参数传:
using System;
namespace Func
{
class Program
{
static void Main(string[] args)
{
Func<int,string> F1 = (p) => "序号:" + p;//实例化一个委托,注意不加大括号,写的值就是返回值,不能带return
Func<string, string> F2 = (p) =>
{
string s = "小道博客:";
s += p;
return s;
};//另一种实例化一个委托,加大括号,可以写多行代码,但必须加上 return
string s1 = S(F1,1);//调用方法
string s2 = S(F2,"http://www.daobk.com");//调用方法
Console.WriteLine(s1);//输出
Console.WriteLine(s2);//输出
Console.ReadKey();//按任意键
}
static string S<T>(Func<T,string> a,T input)//方法
{
return a(input);//返回
}
}
}输出结果:
总结:
Delegate 至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型
Action 可以接受0个至16个传入参数,无返回值
Func 可以接受0个至16个传入参数,必须具有返回值
以上节选自《博客园》
更多参考《博客园》
