zoukankan      html  css  js  c++  java
  • 委托的学习和使用(三种方式)

    委托:

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

    复制代码
    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;
    复制代码

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

  • 相关阅读:
    iOS中Zbar二维码扫描的使用
    SOJ 1135. 飞跃原野
    SOJ 1048.Inverso
    SOJ 1219. 新红黑树
    SOJ 1171. The Game of Efil
    SOJ 1180. Pasting Strings
    1215. 脱离地牢
    1317. Sudoku
    SOJ 1119. Factstone Benchmark
    soj 1099. Packing Passengers
  • 原文地址:https://www.cnblogs.com/2260827114com/p/4929017.html
Copyright © 2011-2022 走看看