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";
            }
        }
    }
  • 相关阅读:
    浅析 c# Queue
    c# Stack操作类
    delegate,event, lambda,Func,Action以及Predicate
    推翻MMSOA与WEBService,使用MEMBRANE soap Monitor检查 wsdl文件。
    JS打印表格(HTML定义格式)
    坑爹的META 刷新页面 框架页面中TOP页面提示刷新 meta元素设置而不基于 JS 的坑爹写法。
    Silverlight载入动画(简易)
    WPF先开了再说
    跨域WEB Service 调用(摘自ASP.NET高级编程第三版)
    安装程序找不到Office.zhcn\OfficeMUI问题
  • 原文地址:https://www.cnblogs.com/xiaowangzhi/p/9441741.html
Copyright © 2011-2022 走看看