当前位置: 代码迷 >> .NET相关 >> 嘱托学习笔记后续:泛型委托及委托中所涉及到匿名方法、Lambda表达式
  详细解决方案

嘱托学习笔记后续:泛型委托及委托中所涉及到匿名方法、Lambda表达式

热度:105   发布时间:2016-04-24 02:57:01.0
委托学习笔记后续:泛型委托及委托中所涉及到匿名方法、Lambda表达式

引言:

  最初学习c#时,感觉委托、事件这块很难,其中在学习的过程中还写了一篇学习笔记:委托、事件学习笔记。今天重新温故委托、事件,并且把最近学习到和委托相关的匿名方法、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;        }    }

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

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;};

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

项目中,使用委托时,很多时候编辑器会帮助我们把方法直接放入合适的委托对象中,但有时候编辑器帮助不了我们,比如:Control.Dispatcher.Invoke(delegate). 例如:

this.btnExit .Dispatcher .Invoke (new Action(() => {}));

 

注:感谢园友上位者的怜悯的意见。

 

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委托,没有参数没有返回值。调用实例为:

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表达式,代码如下: 

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

 

Action<T>

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

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表达式编写Action<T>代码如下:

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

 

Fun<T>

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

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表达式编写Fun<T>,代码如下:

Fun<int, int, bool> fun = (x, y) =>            {                if (x + y == 5)                {                    return true;                }                else                {                    return false;                }            };            bool ret = fun(2,3);

 

Fun<T>没有参数有返回值的情况:

Fun<bool> fun = () =>            {                int x = 4;                int y = 3;                if (x + y == 5)                {                    return true;                }                else                {                    return false;                }            };            //也可以如此编写            Fun<bool> fun = () => false;

此种情况很少使用,而且由于能力有限,暂且看不出来使用它们的意义所在。

 

总结:

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

 

2楼上位者的怜悯
1.“匿名方法构成的委托”,我觉得不太准确,应该是大部分时候编辑器会帮你把方法放入合适的委托,有些地方编辑器是帮不了你的,比如:,Control.Invoke(delegate),这个地方必须放委托,它不能确定你的方法是什么样子的,,2.Funclt;boolgt; func = () =gt; true;,Func委托是可以没有参数的
Re: lhb62232397
@上位者的怜悯,非常感谢提出的意见,已修改。
1楼ChuckLu
有看《C#入门经典》吗?
Re: lhb62232397
@ChuckLu,以前有选择性的看过,what?
  相关解决方案