zoukankan      html  css  js  c++  java
  • C#委托,事件,匿名委托

    作为一个初学者,写下来是当做自己的学习笔记,希望在以后遇到问题的时候能够快速的找到方法

    如果能帮助跟我一样的新人是更好不过的了        如果有什么不正确或者可以改进的地方也希望大家能够指出来  谢谢大家

    1.委托

    来百度一下什么是委托:委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用If-Else(Switch)语句,同时使得程序具有更好的可扩展性。(百度百科)

    先来写个简单的委托:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            //声明没有参数,没有返回值的委托
            public delegate void MyDelegate();
            //委托对象
            static MyDelegate myDelegate;
    
            public static void Show()
            {
                Console.WriteLine("我的代理");
            }
    
            static void Main(string[] args)
            {
                myDelegate = new MyDelegate(Show);//实例化委托  委托myDelegate去做Show的事情
                myDelegate();//调用myDelegate
                Console.ReadKey();
            }
        }
    }
    

    将上面的改为带参数,带返回值的委托

    class Program
        {
            //声明带参数,带返回值的委托
            public delegate string MyDelegate(string data);
            //委托对象
            static MyDelegate myDelegate;
    
            public static string Show(string data)
            {
                return data;
            }
    
            static void Main(string[] args)
            {
                myDelegate = new MyDelegate(Show);//实例化委托  委托myDelegate去做Show的事情
                Console.WriteLine(myDelegate("委托的参数"));//在调用时,需要带上参数
                Console.ReadKey();
            }
        }
    

    现在我们写两个方法

     class Program
        {
            //声明带参数,带返回值的委托
            public delegate void MyDelegate();
            //委托对象
            static MyDelegate myDelegate;
    
            public static void ShowOne()
            {
                Console.WriteLine("委托一号");
            }
    
            public static void ShowTwo()
            {
                Console.WriteLine("委托二号");
            }
        }
    

    我们现在想输出两个中的一个  可以改成这样  顺便复习一下带参数的委托

    class Program
        {
            //声明带参数的委托
            public delegate void MyDelegate(string data);
            //委托对象
            static MyDelegate myDelegate;
    
            public static void Show(string data)
            {
                switch (data)
                {
                    case "委托一号":
                        Console.WriteLine("委托一号");
                        break;
                    case "委托二号":
                        Console.WriteLine("委托二号");
                        break;
                }
            }
    
            static void Main(string[] args)
            {
                myDelegate = new MyDelegate(Show);
                myDelegate("委托一号");//调用时的参数是什么就会输出对应的委托了
                Console.ReadKey();
            }
        }
    

     什么委托一号  什么委托二号 这个好记 要是选项多了  东西又不太好记怎么办  稍微改进一下

    /// <summary>
        /// 1是委托一号  2是委托二号
        /// </summary> 
       enum DATA
        {
            zero,
            NumOne,
            NumTwo,
        };
    
        class Program
        {
            //声明带参数的委托
            public delegate void MyDelegate(DATA data);
            //委托对象
            static MyDelegate myDelegate;
    
            public static void Show(DATA data)
            {
                switch (data)
                {
                    case DATA.NumOne:
                        Console.WriteLine("委托一号");
                        break;
                    case DATA.NumTwo:
                        Console.WriteLine("委托二号");
                        break;
                }
            }
    
            static void Main(string[] args)
            {
                myDelegate = new MyDelegate(Show);
                myDelegate(DATA.NumTwo);
                Console.ReadKey();
            }
        }
    

    是不是瞬间感觉高大上了     还有要活用/// <summary>    /// </summary>      这可是好东西 可以让别人用你代码的时候更加的清楚用的是什么有什么用处     可以提高代码的可读性哦

    2.事件

    如果我们希望两个方法同时输出该怎么做呢    应该这样:

     class Program
        {
            //声明带参数,带返回值的委托
            public delegate void MyDelegate();
            //委托对象
            static event MyDelegate myDelegate;//此处与上面不同哦
    
            public static void ShowOne()
            {
                Console.WriteLine("委托一号");
            }
    
            public static void ShowTwo()
            {
                Console.WriteLine("委托二号");
            }
    
            static void Main(string[] args)
            {
                myDelegate = new MyDelegate(ShowOne);//先给你对象赋上一个方法
                myDelegate += new MyDelegate(ShowTwo);//再给你加上一个方法
                myDelegate();//调用
                Console.ReadKey();
            }
        }
    

    看下输出  调用一个  两个同时都显示出来了  没错这就是事件(event)

    事件可以加 那能不能减呢   聪明  你没有猜错 当然可以  改完后看下结果

     static void Main(string[] args)
            {
                myDelegate = new MyDelegate(ShowOne);//先给你对象赋上一个方法
                myDelegate += new MyDelegate(ShowTwo);//再给你加上一个方法
                myDelegate();//调用
                Console.WriteLine("__________________________我来分割________________________");
                myDelegate -= new MyDelegate(ShowOne);
                myDelegate();//减完后再调一次
                Console.ReadKey();
            }
    

     

    3.匿名方法

    委托和事件说完了  来看看特别一点的    匿名方法

    在之前  每次实例化委托的时候都要有一个已经写好的方法  而匿名方法则不需要

        class Program
        {
            //声明带参数的委托
            public delegate void MyDelegate(string data);
            //委托对象
            static MyDelegate myDelegate;
    
            static void Main(string[] args)
            {
                myDelegate = delegate(string data)         //带参数
                {
                    Console.WriteLine(data);
                };
                myDelegate("带参数的匿名方法");
                Console.ReadKey();
            }
        }
    

     不带参数的只要将参数去掉即可       delegate后面跟上你需要用的参数在{}中进行你需要的操作  最后不要忘记带上分号哦

    4.Lambda表达式

    要是匿名方法还不够快  那试试这个

    class Program
        {
            //声明带参数的委托
            public delegate void MyDelegate(string data);
            //委托对象
            static MyDelegate myDelegate;
    
            static void Main(string[] args)
            {
                myDelegate = (string data) => { Console.WriteLine(data); };
                myDelegate("带参数Lambda表达式");
                Console.ReadKey();
            }
        }
    

     代码量是不是更少了呢  直接用小括号写上参数,然后是=> 再用个{}来框住你的操作  同样最后也有分号

    5.Action

    先来看看它的解释

    参数可以有好多也可以没有  但是  他没有返回值  来看看怎么用吧

    class Program
        {
            //不带参数的Action
            static Action actionOne;
            //带参数的Action
            static Action<string> actionTwo;
    
            static void Main(string[] args)
            {
                actionOne = new Action(ShowOne);
                actionOne();
    
                actionTwo = new Action<string>(ShowTwo);
                actionTwo("Action ShowTwo");
                Console.ReadKey();
            }
    
            static void ShowOne()
            {
                Console.WriteLine("Action ShowOne
    ");
            }
    
            static void ShowTwo(string data)
            {
                Console.WriteLine(data);
            }
        }
    

     试着输出一下吧

    6.Func

    它与Action类似  但是他是必须带返回值的

    前面的都是它的参数, 最后一个参数就是他的返回值

     //不带参数的Action
            static Func<string> FuncOne;
            //带参数的Action
            static Func<string,string> FuncTwo;
    
            static void Main(string[] args)
            {
                FuncOne = new Func<string>(ShowOne);
                Console.WriteLine(FuncOne());
    
                FuncTwo = new Func<string, string>(ShowTwo);
                Console.WriteLine(FuncTwo("Func ShowTwo"));
                Console.ReadKey();
            }
    
            static string ShowOne()
            {
                return "Func ShowOne
    ";
            }
    
            static string ShowTwo(string data)
            {
                return data;
            }
    

     大家试着自己输出吧  毕竟程序还是要多练的哦

  • 相关阅读:
    POJ 3268 Silver Cow Party (Dijkstra)
    怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
    CF Amr and Music (贪心)
    CF Amr and Pins (数学)
    POJ 3253 Fence Repair (贪心)
    POJ 3069 Saruman's Army(贪心)
    POJ 3617 Best Cow Line (贪心)
    CF Anya and Ghosts (贪心)
    CF Fox And Names (拓扑排序)
    mysql8.0的新特性
  • 原文地址:https://www.cnblogs.com/OrangeZhang/p/delegate_2015112.html
Copyright © 2011-2022 走看看