当前位置: 移动技术网 > IT编程>开发语言>c# > C#中委托的+=和-=深入研究

C#中委托的+=和-=深入研究

2019年07月18日  | 移动技术网IT编程  | 我要评论

写在前面

为什么会突然想说说委托?原因吗,起于一个同事的想法,昨天下班的路上一直在想这个问题,如果给委托注册多个方法,会不会都执行呢?为了一探究性,就弄了个demo研究下。

+=

大家都知道委托都继承自system.multicastdelegate,而system.multicastdelegate又继承自system.delegate,可以通过+=为委托注册多个方法。那么他们是否都执行了呢?执行的结果又是怎样的呢?有返回值和没返回值的是否结果是否一样?那就试着说说+=都干了哪些事?

测试代码

复制代码 代码如下:

namespace wolfy.delegatedemo
{
    public delegate void showmsg(string msg);
    public delegate int mathoperation(int a, int b);
    class program
    {
        static showmsg showmsg;
        static mathoperation mathoperation;
        static void main(string[] args)
        {
            showmsg += showhello;
            showmsg += showhello1;
            showmsg("大家新年好啊");
            mathoperation += add; 
            mathoperation += multiply;
            int result = mathoperation(1, 2);
            console.writeline(result.tostring());
            console.read();
        }
        static void showhello(string msg)
        {
            console.writeline("哈喽:" + msg);
        }
        static void showhello1(string msg)
        {
            console.writeline("哈喽1:" + msg);
        }
        static int add(int a, int b)
        {
            return a + b;
        }
        static int multiply(int a, int b)
        {
            return a * b;
        }
    }
}

你可以猜猜运行结果,如下图:

可以看到没有返回值的都输出了,有返回值的只输出了mutiply的结果,那么+=内部做了哪些事?可以看一下反编译的代码:

复制代码 代码如下:

using system;
namespace wolfy.delegatedemo
{
    internal class program
    {
        private static showmsg showmsg;
        private static mathoperation mathoperation;
        private static void main(string[] args)
        {
            program.showmsg = (showmsg)delegate.combine(program.showmsg, new showmsg(program.showhello));
            program.showmsg = (showmsg)delegate.combine(program.showmsg, new showmsg(program.showhello1));
            program.showmsg("大家新年好啊");
            program.mathoperation = (mathoperation)delegate.combine(program.mathoperation, new mathoperation(program.add));
            program.mathoperation = (mathoperation)delegate.combine(program.mathoperation, new mathoperation(program.multiply));
            console.writeline(program.mathoperation(1, 2).tostring());
            console.read();
        }
        private static void showhello(string msg)
        {
            console.writeline("哈喽:" + msg);
        }
        private static void showhello1(string msg)
        {
            console.writeline("哈喽1:" + msg);
        }
        private static int add(int a, int b)
        {
            return a + b;
        }
        private static int multiply(int a, int b)
        {
            return a * b;
        }
    }
}

 通过上面的代码可以看出+=内部是通过委托的 combine静态方法将委托进行组合的,可以看一下委托的这个静态方法是如何实现的。

可以看到最终调用combineimpl这个方法,这个方法内部很奇怪:

并没有我们想看到的代码,那这个方法是干嘛用的啊?

msdn的解释

concatenates the invocation lists of the specified multicast (combinable) delegate and the current multicast (combinable) delegate.

大概意思就是:将当前的委托加入到指定的多播委托集合中。

绕了一圈那么有返回值的委托,到底执行了么?那也只能通过调试来看看了。(绕了一圈,又回到了编辑器,唉)

继续f11你会发现确实进入了add方法

也确实执行了,但在遍历多播委托集合的时候,将之前的值给覆盖了。

那么现在可以得出这样的结论了:无返回值的委托,你给它注册多少个方法,它就执行多少个方法,而有返回值的委托,同样注册多少个方法就执行多少个方法,但返回的是最后一个方法的返回值。

-=

既然说了+=,那么作为收拾烂摊子的-=也不得不说。在项目中使用了+=就要使用-=来释放。那它内部做了哪些事?同样使用上面的代码,在输出结果后,使用-=来释放资源。

可以看出,使用-=内部是调用了委托的remove静态方法。

使用-=最终是将委托置为null,为null另一个意思就是空引用,这样就可以等待垃圾回收器进行回收了。

总结

这个问题虽然很基础,一个同事当时问了,就给他说了一下,在下班的路上一直在想,内部是如何实现的?就试着通过反编译的方式一探究竟。但貌似combineimpl这个方法,给的结果不太满意。没看到具体的实现。希望对你有所帮助!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网