zoukankan      html  css  js  c++  java
  • C# 委托、事件

    委托(delegate)

        访问修饰符 delegate 返回值类型 委托名 (参数列表)

        委托是一种可以把引用存储为函数的类型,也就是说它声明了一种用于保存特定格式函数的数据类型,如图C++中的函数指针。

        1.匿名委托

        委托类型 实例化名 = delegate(参数列表){函数体}

        2.泛型委托

        delegate T1 委托名<T1, T2>(T1 v1, T2 v2);

        3.委托的多播性

        委托类型 实例化名 += 注册函数

        委托类型 实例化名 -= 解除函数

        一个实例化委托不仅可以注册一个函数还可以注册多个函数,注册多个函数后,在执行委托的时候会根据注册函数的注册先后顺序依次执行每一个注册函数。

    如果委托是有返回值的,则返回的是最后一个函数的返回值。

    注意:如果对已经注册了函数的委托实例从新使用=号赋值,相当于从新实例化。

    事件

        访问修饰符 event 委托类型 事件名

        事件类似于异常,因为它们都是由对象引发,我们可以提供代码来处理事件。单个事件可供多个处理程序订阅,在该程序发生时,这些处理程序都会被调用。

    事件注册和解除函数操作与委托一样。其实事件就是对委托的封装,就如同c#类中属性对字段的封装一样,其封装后可以在委托上封装更复杂的逻辑。在事件被编译后自动生成了个private的委托实例和两个函数add_checkEvent和remove_checkEvent,这两个函数分别对应事件的+=/-=操作

        1.隐式声明事件

        event 委托类型 事件名

        2.显示声明事件

    event 委托类型 事件名

    {

       add

       {

           //将函数注册到自己定义的委托实例

       }

       remove

       {

           //解除函数对自己定义的委托实例的注册

       }

    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 事件
    {
        class delegateTest
        {
            public delegate bool Comparer(int v1, int v2);
            public Comparer Compare;
    
            public bool Max(int v1, int v2) { return v1 > v2; }
            public bool Min(int v1, int v2) { return v1 < v2; }
                          
            /// <summary>
            /// 匿名委托
            /// </summary>
            private Comparer myCompare = delegate(int v1, int v2)
            {
                return v1 == v2;
            };
    
            /// <summary>
            /// 泛型委托
            /// </summary>
            public delegate T2 _Com<T1, T2>(T1 v1, T1 v2);
    
            public void sort(int[] arr, Comparer com)
            {
                for (int i = 0; i < arr.GetLength(0); i++)
                {
                    for (int j = 0; j < arr.GetLength(0) - i - 1; j++)
                    {
                        if (!com(arr[j], arr[j + 1]))
                        {
                            int iTemp = arr[j];
                            arr[j] = arr[j + 1];
                            arr[j + 1] = iTemp;
                        }
                    }
                }
            }
        }
    }

    客户端代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Timers;
    
    namespace 事件
    {
        class Program
        {
            public static event delegateTest.Comparer eventComparer;
            static void Main(string[] args)
            {
                int[] arr = new int[] { 1, 2, 3, 4, 6, 7, 87 };
    
                delegateTest test = new delegateTest();
                test.sort(arr, test.Max);
                foreach (int v in arr)
                {
                    Console.Write("{0} ", v);
                }
    
                ///////////////////////////////////////////////
                Console.WriteLine();
                test.Compare = hello;
                test.Compare += world;
                //注册了多个函数的委托,返回值为最后运行的函数的返回值。
                Console.WriteLine(test.Compare(1, 2));
    
                //泛型委托实例化
                delegateTest._Com<int, bool> compare = test.Max;
    
                ///////////////////////////////////////////////
                eventComparer = hello;
                eventComparer += world;
                eventComparer(1, 2);
            }
    
            static bool hello(int v1, int v2)
            {
                Console.Write("hello ");
                return false;
            }
            static bool world(int v1, int v2)
            {
                Console.WriteLine("wrold!");
                return true;
            }
        }
    }
  • 相关阅读:
    实验9: 静态路由和默认路由
    实验8:路由器IOS升级2
    实验7:交换机IOS升级
    实验6:路由器IOS升级
    实验5: IOS的升级与恢复
    实验4: 路由器的密码恢复
    实验3: DHCP 基本配置
    Linux用户的基本操作3 (组的基本管理,用户提权)
    拓展练习:(用户的基本管理及用户提权部分)
    linux用户的基本操作2 用户密码管理
  • 原文地址:https://www.cnblogs.com/wrbxdj/p/5667354.html
Copyright © 2011-2022 走看看