美文网首页
C# 委托三种使用

C# 委托三种使用

作者: 赌二八定律 | 来源:发表于2022-03-22 09:19 被阅读0次
不带返回值的委托
        public delegate void DelegateDemo(String s1, String s2);
        public static event DelegateDemo ddo;

        public class Test1
        {
            public void Look1(String s1, String s2)
            {
                Console.WriteLine("Look1  " + s1 + "  -1-  " + s2);
            }
            public void Look2(String s1, String s2)
            {
                Console.WriteLine("Look2  " + s1 + "  -2-  " + s2);
            }
            public void Look3(String s1, String s2)
            {
                Console.WriteLine("Look3  " + s1 + "  -3-  " + s2);
            }
        }
        

        static void Main(string[] args)
        {
            Test1 test1 = new Test1();
            //方式一 不定义事件event
            DelegateDemo dd = new DelegateDemo(test1.Look1);
            dd += new DelegateDemo(test1.Look2);
            dd += new DelegateDemo(test1.Look3);
            dd += new DelegateDemo(test1.Look3);
            dd += new DelegateDemo(test1.Look3);
            dd("111","222");
            dd("333","444");

            //方法二 先定义好事件event
            ddo += new DelegateDemo(test1.Look1);
            ddo("555","666");

            Console.ReadKey();

        }

Look1 111 -1- 222
Look2 111 -2- 222
Look3 111 -3- 222
Look3 111 -3- 222
Look3 111 -3- 222
Look1 333 -1- 444
Look2 333 -2- 444
Look3 333 -3- 444
Look3 333 -3- 444
Look3 333 -3- 444
Look1 555 -1- 666

带参数返回值的阻塞委托
//委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用If-Else(Switch)语句,同时使得程序具有更好的可扩展性。
//委托:具有 同样参数和返回值 的函数的集合.
        public delegate string MyDelegate(int arg, string str);
        public static string MyFunction1(int count, string str)
        {
            Debug.Print("count: "+count+"  str: "+str);
            for (int i=0;i<3;i++)
            {
                Thread.Sleep(1000 * 3);
                Debug.Print("---sleep---");
            }
            return "MyFunction1";
        }
        private void Load()
        {
            
            MyDelegate dele = new MyDelegate(MyFunction1);
            IAsyncResult res = dele.BeginInvoke(10, "abcd", null, null);
            string result = dele.EndInvoke(res);
            Debug.Print("result: "+result);

            IAsyncResult res2 = dele.BeginInvoke(20, "efgh", null, null);
            string result2 = dele.EndInvoke(res2);
            Debug.Print("result2: " + result2);

            Debug.Print("END");
        }

count: 10 str: abcd
---sleep---
---sleep---
---sleep---
result: MyFunction1
count: 20 str: efgh
---sleep---
---sleep---
---sleep---
result2: MyFunction1
END
//该方法会阻塞在EndInvoke方法

带参数返回值的非阻塞委托
public delegate string MyDelegate(int arg, string str);
        public static string MyFunction1(int count, string str)
        {
            Debug.Print("count: "+count+"  str: "+str);
            for (int i=0;i<3;i++)
            {
                Thread.Sleep(1000 * 3);
                Debug.Print("---sleep---");
            }
            return "MyFunction1";
        }
        private void Load()
        {
            MyDelegate dele = new MyDelegate(MyFunction1);
            dele.BeginInvoke(10, "abcd", CallBackFun, dele);
            Debug.Print("END");
        }

        public void CallBackFun(IAsyncResult ar)
        {
            MyDelegate caller = ar.AsyncState as MyDelegate;
            string result = caller.EndInvoke(ar);
            Debug.Print("result: " + result);
            Debug.Print("Completed!");
        }

count: 10 str: abcd
END
---sleep---
---sleep---
---sleep---
result: MyFunction1
Completed!

相关文章

  • C# 委托三种使用

    不带返回值的委托 Look1 111 -1- 222Look2 111 -2- 222Look3 1...

  • 教小明学一点点编程—C#篇(0)

    像小明这样使用C#的新同学,一定都会被C#的复杂的委托事件机制所困惑,那么我们不如解释一下什么是C#中委托。 委托...

  • C#委托 匿名函数 Lambda

    C# 中委托的发展#在 C# 1.0 中,通过使用在代码中其他位置定义的方法显式初始化委托来创建委托的实例。 C#...

  • 委托及其用法

    C#委托使用详解(Delegates) 1. 委托是什么? 其实,我一直思考如何讲解委托,才能把委托说得更透彻。说...

  • C#匿名函数

    在 2.0 之前的 C# 版本中,声明委托的唯一方式是使用命名方法。 C# 2.0 引入匿名方法,在 C# 3.0...

  • C# 委托

    C#委托 C#中的委托(Delegate)类似于C或C++中函数的指针。委托(Delegate)是存有对某个方法的...

  • C#—委托的使用

    委托 委托实际为一种 中间人 ,A需要C去做某件事情 而由B去调用C来做事。 1.委托是一种类型,它可以声明在类的...

  • C#基础提升系列——C#委托

    C# 委托 委托是类型安全的类,它定义了返回类型和参数的类型,委托类可以包含一个或多个方法的引用。可以使用lamb...

  • 2017-08-05学习日迹

    C#委托基本使用 1,声明委托(相当于中介)(定义一个函数的原型:返回值+参数类型和个数) public dele...

  • 2020-02-19

    C# 委托 (一)—— 委托、 泛型委托与Lambda表达式 原创wnvalentin 最后发布于2018-08-...

网友评论

      本文标题:C# 委托三种使用

      本文链接:https://www.haomeiwen.com/subject/kforlrtx.html