What is the difference between a nested method call and delegates?

consider the 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 ... the same thing will happen when I call the delegate ... so using the delegate to handle some kind of event when I can use the 1st approach .. The 1st approach also encapsulates a method call.

+4
source share
2 answers

In the case of delegation, the order of calling connected functions is not specified.

In addition, you can attach to it any number of functions, even at runtime, from other objects, and not just from hardcoded ones, as in the first approach. The delegate has wider use.

+6
source

The first approach is static. The delegate approach allows you or the caller to determine what is called later.

+1
source

All Articles