zoukankan      html  css  js  c++  java
  • 委托和事件

    一、委托的使用

      1:首先来看看委托的使用

        1)传统的委托使用的分为三步。

     1         /// <summary>
     2         /// 定义委托
     3         /// </summary>
     4         /// <param name="x">变量 x</param>
     5         /// <param name="y">变量 y</param>
     6         /// <returns>返回一个 Int 值</returns>
     7         public delegate int IntDelegate(int x, int y);
     8         
     9         /// <summary>
    10         /// Add 静态方法
    11         /// </summary>
    12         /// <param name="x">参数 x</param>
    13         /// <param name="y">参数 y</param>
    14         /// <returns>返回 Int 值</returns>
    15         public static int Add(int x, int y)
    16         {
    17             return x + y;
    18         }
    19 
    20         /// <summary>
    21         /// 入口函数
    22         /// </summary>
    23         /// <param name="args"></param>
    24         public static void Main(string[] args)
    25         {
    26             IntDelegate delegt = new IntDelegate(Add);
    27             Console.WriteLine(delegt.Invoke(3, 9));
    28         }
    View Code

        2)以下的三种方式展示两步解决委托的使用

     1         /// <summary>
     2         /// 实例化委托类
     3         /// </summary>
     4         public static void Test2()
     5         {
     6             IntDelegate delegt = delegate(int x, int y) { return x + y; };
     7             Console.WriteLine(delegt(4, 10));
     8         }
     9 
    10         /// <summary>
    11         /// 在上面的方法的基础上去掉 delegate
    12         /// </summary>
    13         public static void Test3()
    14         {
    15             IntDelegate delegt = (int x, int y) => { return x + y; };
    16             Console.WriteLine(delegt.Invoke(4, 50));
    17         }
    18 
    19         /// <summary>
    20         /// 在上面的方法的基础上去掉 delegate 和 return 
    21         /// </summary>
    22         public static void Test4()
    23         {
    24             IntDelegate delegt = (x, y) => (x + y);
    25             Console.WriteLine(delegt.Invoke(4, 50));
    26         }
    View Code

          

      2:再来看看 .NET 自己封装的委托— Action 、 Func 

        1)其区别: Action 是无返回值的;Func 是有返回值的,且返回值的类型是其最后一个参数类型(学艺不精,大牛请指教)

        2)下面是 Func 的使用,下面的例子只支持2个参数,但是同样支持4个参数

     1         /// <summary>
     2         /// Add 静态方法
     3         /// </summary>
     4         /// <param name="x">参数 x</param>
     5         /// <param name="y">参数 y</param>
     6         /// <returns>返回 Int 值</returns>
     7         public static int Add(int x, int y)
     8         {
     9             return x + y;
    10         }
    11 
    12         /// <summary>
    13         /// Func 的使用,第三个Int 是返回值类型
    14         /// </summary>
    15         public static void Test5()
    16         {
    17             Func<int, int, int> func = (x, y) => Add(x, y);
    18             Console.WriteLine(func.Invoke(102, 204));
    19         }
    View Code

        3)下面是 Action 的使用

     1         /// <summary>
     2         /// Sub 静态方法
     3         /// </summary>
     4         /// <param name="x"></param>
     5         /// <param name="y"></param>
     6         public static void Sub(int x, int y)
     7         {
     8             Console.WriteLine(x - y);
     9         }
    10 
    11         /// <summary>
    12         /// Action 的使用
    13         /// </summary>
    14         public static void Test6()
    15         {
    16             Action<int, int> action = Sub;
    17             action.Invoke(101, 94);
    18         }
    View Code

    二、事件的使用

     1     /// <summary>
     2     /// 委托
     3     /// </summary>
     4     /// <param name="str">委托参数</param>
     5     public delegate void Show(string str);
     6     public class Program
     7     {
     8         /// <summary>
     9         /// 事件
    10         /// </summary>
    11         public static event Show ShowEvent;
    12 
    13         /// <summary>
    14         /// 方法 ShowImg
    15         /// </summary>
    16         /// <param name="strImg"></param>
    17         public static void ShowImg(string strImg)
    18         {
    19             Console.WriteLine("strImg: " + strImg);
    20         }
    21 
    22         /// <summary>
    23         /// 方法 ShowMusic
    24         /// </summary>
    25         /// <param name="strMusic"></param>
    26         public static void ShowMusic(string strMusic)
    27         {
    28             Console.WriteLine("strMusic: " + strMusic);
    29         }
    30         static void Main(string[] args)
    31         {
    32             ShowEvent += ShowImg;
    33             Console.WriteLine(ShowEvent.GetInvocationList().Count());
    34             ShowEvent += ShowMusic;
    35             Console.WriteLine(ShowEvent.GetInvocationList().Count());
    36             ShowEvent("图片");
    37             ShowEvent("音乐");
    38         }
    39     }
    View Code

    细心的童鞋可能发现:

    在调用的时候,我有时用委托构造方法,有时调用委托的虚方法 invoke 方法

    其实是一样的,但是我还不知道为什么。。。  >..<...|| (大神指教)

      

          

  • 相关阅读:
    fedora上部署ASP.NET——(卡带式电脑跑.NET WEB服务器)
    SQL Server 请求失败或服务未及时响应。有关详细信息,请参见事件日志或其它适合的错误日志
    8086CPU的出栈(pop)和入栈(push) 都是以字为单位进行的
    FTP 服务搭建后不能访问问题解决
    指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配
    Linux 安装MongoDB 并设置防火墙,使用远程客户端访问
    svn Please execute the 'Cleanup' command. 问题解决
    .net 操作MongoDB 基础
    oracle 使用绑定变量极大的提升性能
    尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题。
  • 原文地址:https://www.cnblogs.com/Jacob-Wu/p/5773760.html
Copyright © 2011-2022 走看看