什么是嵌套的方法调用和代表之间的区别? [英] Whats is the difference between nested method call and delegates?

查看:192
本文介绍了什么是嵌套的方法调用和代表之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下方面:

 

code> public void f3()
{
f2();
f1();
}

和此...



第二种方法

  class Sample 
{
public delegate void MyDelegate(string s);
MyDelegate obj;

public Sample()
{
obj + = new MyDelegate(input);
obj + = new MyDelegate(something);
obj + = new MyDelegate(someStaticMethod);
}
}

当我调用f3列出里面...同样会发生,当我将调用委托...所以什么使用委托处理一些事件,当我可以使用第一种方法... 第一种方法也封装了方法调用..

解决方案

如果是委托,则不指定附加函数的调用顺序。



此外,你可以附加任意数量的函数,甚至在运行时,从其他对象,而不只是硬编码的,如第一种方法。代表有更广泛的使用。


consider following:

1st APPROACH:

public void f3()
{
f2();
f1();
}

and this ...

2nd APPROACH:

    class Sample
{
    public delegate void MyDelegate(string s);
    MyDelegate obj;

    public Sample()
    {
        obj += new MyDelegate(input);
        obj+=new MyDelegate(something);
        obj += new MyDelegate(someStaticMethod);
    }
}

When i call f3() it will call the functions listed inside it ... same would happen when i will invoke a delegate ... so whats the use of delegate to handle some event when i can use 1st approach ... the 1st approach too encapsulates the method call..

解决方案

In case of the delegate, the invocation order of the attached functions is not specified.

Also, you can attach any number of functions to it, even during runtime, from other objects, not just the hard coded ones as in the first approach. The delegate has wider usage.

这篇关于什么是嵌套的方法调用和代表之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆