zoukankan      html  css  js  c++  java
  • 委托的自己理解

    面试的时候估计都会被问过,什么是委托,事件是不是一种委托?委托的优点都是什么?我在项目中经常使用,但是平时不注意整理概念性知识,回答起来像是囫囵吞枣,答不出个所以然来。今天周末抽出来一些时间,静下心来整理下。下面我将采用一问一答的性质来整理和记录。

    1.什么是委托?

    委托是一种类型安全的对象,它是指向程序中的以后会被调用的函数方法(可以是多个)。

    2.事件是不是一种委托?

    是,是一种特殊的委托。

    3.委托怎么创建?

    1 // 委托类型包含三个主要信息:
    2 // 1.它所调用的方法名称。
    3 // 2.调用方法的参数(可选)。
    4 // 3.该方法的返回类型(可选)。
    5 public delegate string Texts(int x, int y);

    4.委托的性质分为几种?

    委托性质分为两种,分别是异步委托,同步委托。

    5.创建委托后,程序运行的时候发生了什么?

    编译器处理委托对象时候,会自动产生一个派生自System.MulticastDelegate的密封类,它和它的基类System.Delegate。一起为委托提供必要的基础设施。

    通过ildasm.exe我们查看刚才创建的Texts委托,发现它定义了3个公共方法。

    1 public string Invoke(int x, int y);
    2 public IAsyncResult BeginInvoke(int x, int y, AsynCallback cb, object state);
    3 public int EndInvoke(IAsyncResult result);

    可以看出BeginInvoke用于异步调用。

    6.能不能写一个实战的例子?

     1 class Program
     2     {
     3         //声明一个委托,这个委托可以指向任何传入两个int类型参数,并返回int类型的方法
     4         public delegate int Texts(int x, int y);
     5         public class SimpleMath
     6         {
     7             public static int Add(int x, int y)
     8             {
     9                 return x + y;
    10             }
    11             public static int Subtract(int x, int y)
    12             {
    13                 return x - y;
    14             }
    15 
    16         }
    17         static void Main(string[] args)
    18         {
    19             //创建一个指向SimpleMath类中Add方法的对象
    20             Texts t = new Texts(SimpleMath.Add);
    21 
    22             //使用委托对象间接调用Add方法
    23             Console.WriteLine("2+3={0}", t(2, 3));
    24             Console.ReadLine();
    25 
    26         }
    27     }
    View Code

    7.什么是委托的多播。

    委托的多播可以理解为,创建一个委托对象可以维护一个可调用方法的列表而不只是一个单独方法。   直接上代码可能更加直观。

     1 class Program
     2     {
     3         //声明一个委托,这个委托可以指向任何传入两个int类型参数,并返回int类型的方法
     4         public static int a = 0;
     5         public delegate int Texts(int x, int y);
     6         public class SimpleMath
     7         {
     8             public static int Add(int x, int y)
     9             {
    10                 return a = x + y;
    11             }
    12             public static int Subtract(int x, int y)
    13             {
    14                 return a = x + y + a;
    15             }
    16 
    17         }
    18         static void Main(string[] args)
    19         {
    20             //创建一个指向SimpleMath类中Add方法的对象
    21             Texts t = new Texts(SimpleMath.Add);
    22             //委托的多播,可以直接使用+=
    23             t += SimpleMath.Subtract;
    24             t(2, 3);
    25             //使用委托对象间接调用Add和Subtract方法
    26             Console.WriteLine("a=" + a + "");
    27             Console.ReadLine();
    28 
    29         }
    30     }
    View Code

    8.有没有快速创建委托的办法?

    我们可以使用Action<>和Func<>快速创建委托。

     1         public static void TextAction(int x, int y)
     2         {
     3             Console.WriteLine("x=" + x + ",y=" + y + "");
     4         }
     5         static void Main(string[] args)
     6         {
     7 
     8             //使用Action<>泛型委托快捷创建一个指向 TextAction 方法的委托。   (注意Action 只能指向没有返回值的方法)
     9             Action<int, int> action = new Action<int, int>(TextAction);
    10             action(1, 2);
    11 
    12         }
    View Code
     1         public static int Textfun(int x, int y)
     2         {
     3             return x + y;
     4         }
     5         static void Main(string[] args)
     6         {
     7 
     8             //使用fun<>泛型委托快捷创建一个指向 Textfun 方法的委托。   (注意Func 最后一个参数值得是方法的返回值类型)
     9             Func<int, int, int> fun = new Func<int, int, int>(Textfun);
    10             Console.Write(fun.Invoke(2, 3));
    11 
    12         }
    View Code

    暂时先整理这么多,有哪块错误或遗漏,欢迎大家指出和补充。

      

  • 相关阅读:
    CentOS7 安装 Mysql 服务
    git 第一次 push 遇到问题
    为什么PHP(CLI)同一个错误信息会打印两次?
    python密码输入模块getpass
    Linux安装JDK详细步骤
    嘿嘿嘿,开始自学mysql
    Bable实现由ES6转译为ES5
    AJAX
    模板层
    lshw查看系统硬件信息
  • 原文地址:https://www.cnblogs.com/xiaobaicode/p/11939122.html
Copyright © 2011-2022 走看看