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

    using System;
    
    namespace Test
    {
        delegate void InformDelegate(string from);
    
        class Program
        {
            static void Main(string[] args)
            {
                new Regist().Invoke1();
                new Regist().Invoke2();
                new Regist().Invoke3();
            }
        }
    
        class Regist
        {
            private event InformDelegate InformEvent;// 相当于委托的实例
    
            private void InformEvent1(string from)
            {
                Console.WriteLine($"from {from}, InformEvent1");
            }
    
            private void InformEvent2(string from)
            {
                Console.WriteLine($"from {from}, InformEvent2");
            }
    
            private void InformEvent3(string from)
            {
                Console.WriteLine($"from {from}, InformEvent3");
            }
    
            public void Invoke1()
            {
                InformEvent += InformEvent1;
                InformEvent.Invoke("lily");
            }
    
            public void Invoke2()
            {
                InformEvent += InformEvent1;
                InformEvent += InformEvent2;
                InformEvent.Invoke("lily");
            }
    
            public void Invoke3()
            {
                InformEvent += InformEvent1;
                InformEvent += InformEvent2;
                InformEvent += InformEvent3;
                InformEvent.Invoke("lily");
            }
        }
    }
    using System;
    
    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                new Regist().Invoke0();
                new Regist().Invoke1();
                new Regist().Invoke2();
                new Regist().Invoke3();
            }
        }
    
        class Regist
        {
            private event Action ActionEvent0;// 内置的泛型委托(无返回值)
            private event Action<string> ActionEvent1;
            private event Action<string, string> ActionEvent2;
            private event Action<string, string, string> ActionEvent3;
    
            private void Regist_ActionEvent0()
            {
                Console.WriteLine($"Regist_ActionEvent0");
            }
    
            private void Regist_ActionEvent1(string arg1)
            {
                Console.WriteLine($"{arg1}, Regist_ActionEvent1");
            }
    
            private void Regist_ActionEvent2(string arg1, string arg2)
            {
                Console.WriteLine($"{arg1}, {arg2}, Regist_ActionEvent2");
            }
    
            private void Regist_ActionEvent3(string arg1, string arg2, string arg3)
            {
                Console.WriteLine($"{arg1}, {arg2}, {arg3}, Regist_ActionEvent2");
            }
    
            public void Invoke0()
            {
                ActionEvent0 += Regist_ActionEvent0;
                ActionEvent0.Invoke();
            }
    
            public void Invoke1()
            {
                ActionEvent1 += Regist_ActionEvent1;
                ActionEvent1.Invoke("arg1");
            }
    
            public void Invoke2()
            {
                ActionEvent2 += Regist_ActionEvent2;
                ActionEvent2.Invoke("arg1", "arg2");
            }
    
            public void Invoke3()
            {
                ActionEvent3 += Regist_ActionEvent3;
                ActionEvent3.Invoke("arg1", "arg2", "arg3");
            }
        }
    }
    using System;
    
    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                new Regist().Invoke0();
                new Regist().Invoke1();
                new Regist().Invoke2();
                new Regist().Invoke3();
            }
        }
    
        class Regist
        {
            private event Func<string> FuncEvent0;// 内置的泛型委托(有返回值,最后为返回类型)
            private event Func<string, string> FuncEvent1;
            private event Func<string, string, string> FuncEvent2;
            private event Func<string, string, string, string> FuncEvent3;
    
            private string Regist_FuncEvent0()
            {
                return $"Regist_FuncEvent0";
            }
    
            private string Regist_FuncEvent1(string arg1)
            {
                return $"{arg1}, Regist_FuncEvent1";
            }
    
            private string Regist_FuncEvent2(string arg1, string arg2)
            {
                return $"{arg1}, {arg2} Regist_FuncEvent2";
            }
    
            private string Regist_FuncEvent3(string arg1, string arg2, string arg3)
            {
                return $"{arg1}, {arg2}, {arg3} Regist_FuncEvent3";
            }
    
            public void Invoke0()
            {
                FuncEvent0 += Regist_FuncEvent0;
                var result = FuncEvent0.Invoke();
                Console.WriteLine(result);
            }
    
            public void Invoke1()
            {
                FuncEvent1 += Regist_FuncEvent1;
                var result = FuncEvent1.Invoke("arg1");
                Console.WriteLine(result);
            }
    
            public void Invoke2()
            {
                FuncEvent2 += Regist_FuncEvent2;
                var result = FuncEvent2.Invoke("arg1", "arg2");
                Console.WriteLine(result);
            }
    
            public void Invoke3()
            {
                FuncEvent3 += Regist_FuncEvent3;
                var result = FuncEvent3.Invoke("arg1", "arg2", "arg3");
                Console.WriteLine(result);
            }
        }
    }
    using System;
    
    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                new Regist().Invoke();
            }
        }
    
        class Regist
        {
            private event Predicate<string> PredicateEvent;// 内置的泛型委托(只接受一个参数,返回值必须布尔值,即Func<T, bool>)
    
            public void Invoke()
            {
                PredicateEvent += Regist_PredicateEvent; ;
                var result = PredicateEvent.Invoke("arg1");
                Console.WriteLine(result);
            }
    
            private bool Regist_PredicateEvent(string arg1)
            {
                return arg1 == "arg1";
            }
        }
    }
  • 相关阅读:
    "字符反向拼接"组件:<reverse> —— 快应用组件库H-UI
    "首字母变大写"组件:<capitalize> —— 快应用组件库H-UI
    "字母全变大写"组件:<uppercase> —— 快应用组件库H-UI
    "字母全变小写"组件:<lowercase> —— 快应用组件库H-UI
    "多行文本"组件:<multi> —— 快应用组件库H-UI
    "斜体显示"组件:<i> —— 快应用组件库H-UI
    捕捉AVPlayerViewController 系统原生工具栏的出现、隐藏事件
    (简单实用)Android支付宝商家收款语音播报
    使用wkwebview时,push后,再pop返回,报错
    安卓进度条两边圆角+渐变的拓展
  • 原文地址:https://www.cnblogs.com/xiaowangzhi/p/9441741.html
Copyright © 2011-2022 走看看