泛型委托及委托中所涉及到匿名方法、Lambda表达式

泛型委托及委托中所涉及到匿名方法、Lambda表达式

引言:

  最初学习c#时,感觉委托、事件这块很难,其中在学习的过程中还写了一篇学习笔记:委托、事件学习笔记。今天重新温故委托、事件,并且把最近学习到和委托相关的匿名方法、Lambda表达式及泛型委托记录下来,以备复习使用。

委托:

  日常工作中,常常见到委托用在具体的项目中。而且委托使用起来相对来说也是非常简单的,下面列举一个委托实例用以说明如何使用委托,代码如下:

泛型委托及委托中所涉及到匿名方法、Lambda表达式
class Program
{
public delegate int CalculateDelegate(int x, int y); static void Main(string[] args)
{
CalculateDelegate calculateDelegate = new CalculateDelegate(Add);
int result = calculateDelegate(2, 3);
} public static int Add(int x, int y)
{
return x + y;
}
}
泛型委托及委托中所涉及到匿名方法、Lambda表达式

从上例中可以看出,使用一个委托分为三个步骤:

1)声明一个委托:public delegate int CalculateDelegate(int x, int y);

2)定义一个委托对象并绑定方法:CalculateDelegate calculateDelegate = new CalculateDelegate(Add);

3)调用委托:int result = calculateDelegate(2, 3);

其中第二步、第三步的写法和大家有出入,通常很多人喜欢这样写:

1)声明一个委托

2)定义一个委托要绑定的方法

3)定义一个委托,绑定上述定义的方法

匿名方法:

使用上面编写的委托实例,描述匿名方法到底为何物,是怎么使用的。委托绑定方法实现如下:

CalculateDelegate calculateDelegate = new CalculateDelegate(Add);

public static int Add(int x, int y)
{
return x + y;
}

如果使用匿名方法重写上面的方法,代码如下:

CalculateDelegate calculateDelegate = delegate(int x, int y){return x + y;};

可见:匿名方法绑定委托直接省去了编写一个单独的方法,使得代码更为简洁。

Lambda表达式:

使用Lambda表达式重写上面的匿名方法构成的委托,在匿名方法的基础上,编写如下:

方式一:

CalculateDelegate calculateDelegate = (int x, int y) => { return x + y; };

方式二:

CalculateDelegate calculateDelegate = (x, y) => { return x + y; };

方式三:

CalculateDelegate calculateDelegate = (x, y) => x + y;

从上面可以看出,Lambda仅仅是在匿名方法的基础上加上 => 符号,但是却让整个代码实现起来显得更为优雅。

泛型委托:

在.net平台下有Microsoft自带的泛型委托,如:Action,Action<T>,Fun<T>等。实际使用中,如果需要用到泛型委托,系统内置的委托基本上就能满足需求了,下面一一介绍它们的原型及调用实例。

Action

系统封装的Action委托,没有参数没有返回值。调用实例为:

泛型委托及委托中所涉及到匿名方法、Lambda表达式
class Program
{
public delegate void Action(); static void Main(string[] args)
{
Action action = new Action(Method);
action();
} private static void Method()
{
Console.WriteLine("i am is a action");
}
}
泛型委托及委托中所涉及到匿名方法、Lambda表达式

如果方法的表达很简单,可以使用Lambda表达式,代码如下:

Action action = () => { Console.WriteLine("i am is a action"); };
action();

Action<T>

系统封装的Action<T>委托,有参数但是没有返回值。调用实例为:

泛型委托及委托中所涉及到匿名方法、Lambda表达式
class Program
{
public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2); static void Main(string[] args)
{
Action<int,int> action = new Action<int,int>(Method);
action(2,3);
} private static void Method(int x,int y)
{
Console.WriteLine("i am is a action");
}
}
泛型委托及委托中所涉及到匿名方法、Lambda表达式

使用Lambda表达式编写Action<T>代码如下:

Action<int, int> action = (x, y) => { Console.WriteLine("i am is a action"); };
action(2,3);

Fun<T>

系统封装的Fun<T>委托,有参数而且返回值。调用实例为:

泛型委托及委托中所涉及到匿名方法、Lambda表达式
class Program
{
public delegate TResult Fun<in T1, in T2, out TResult>(T1 arg1, T2 arg2); static void Main(string[] args)
{
Fun<int, int, bool> fun = new Fun<int, int, bool>(Method);
bool ret = fun(2,3);
} private static bool Method(int x,int y)
{
if (x + y == 5)
{
return true;
}
else
{
return false;
}
}
}
泛型委托及委托中所涉及到匿名方法、Lambda表达式

使用Lambda表达式编写Fun<T>,代码如下:

泛型委托及委托中所涉及到匿名方法、Lambda表达式
Fun<int, int, bool> fun = (x, y) =>
{
if (x + y == 5)
{
return true;
}
else
{
return false;
}
}; bool ret = fun(2,3);
泛型委托及委托中所涉及到匿名方法、Lambda表达式

总结:

  在平时的项目中,常见的委托种类及实例本质上也是上述所说的几种。除此之外还有一些其他的委托方法,资质有限精力有限,此处暂且不提,以后碰到在另当别论。

 
 
标签: C#琐碎
上一篇:LINUX安装好后无法访问网络


下一篇:升级了git版本后git clone报ssl错误的解决方法