none
When to use delegates instead of object of the class to call a method ? RRS feed

  • Question

  • Hi all,

    When to use delegates instead of calling a method using the object of that class.

    for example

    i have a class named NewClass()

    it has a method named cls()

    i can invoke the method by simpy creating the object of that class ?

    NewClass n = new NewClass();

    n.cls();

    The delegate will also point the method like

    mydelegate del = new mydelegate(n.cls)

    del();

    both are doing the same i think . so can you explain what is the advantage of using delegates ?


    MSDN Forum

    Thursday, January 17, 2013 3:56 AM

Answers

  • If you want to invoke a method which will be known only at runtime, then you can use delegates. To give a simple example, when you click a button, the Click event will fire. This will in turn invoke EventHandler delegate. You can observe here that Click event doesn't know which method it is going to inveoke, right? So, you can see the usefulness of delegates here.

    So, in general you can say, delegates are used to achieve one or other kind of Observer Pattern.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Thursday, January 17, 2013 7:58 AM
  • Hi,
    when you are encapsulating the method inside a delegate object you get the ability to pass it as a method argument or return from a method, this way you can pass any logic to a method(also known as Helper methods).A lot of examples you can see in LINQ methods.

    mydelegate del = new mydelegate(n.cls)
    HelperMethod(del);
    
    public void HelperMethod(mydelegate del)
    {
     del.Invoke();
    }
    

    HelperMethod will perform some task according to the logic passed to it(through delegate object).
    To better understand i suggest please go through some of the LINQ methods e.g., Where() method.
    Generally you can pass lambda expression to these but actually those lambda are converted to delegate object to appropiate object by the compiler.

    Hope this helps.


    One good question is equivalent to ten best answers.

    Thursday, January 17, 2013 6:48 AM

All replies

  • Hi,
    when you are encapsulating the method inside a delegate object you get the ability to pass it as a method argument or return from a method, this way you can pass any logic to a method(also known as Helper methods).A lot of examples you can see in LINQ methods.

    mydelegate del = new mydelegate(n.cls)
    HelperMethod(del);
    
    public void HelperMethod(mydelegate del)
    {
     del.Invoke();
    }
    

    HelperMethod will perform some task according to the logic passed to it(through delegate object).
    To better understand i suggest please go through some of the LINQ methods e.g., Where() method.
    Generally you can pass lambda expression to these but actually those lambda are converted to delegate object to appropiate object by the compiler.

    Hope this helps.


    One good question is equivalent to ten best answers.

    Thursday, January 17, 2013 6:48 AM
  • If you want to invoke a method which will be known only at runtime, then you can use delegates. To give a simple example, when you click a button, the Click event will fire. This will in turn invoke EventHandler delegate. You can observe here that Click event doesn't know which method it is going to inveoke, right? So, you can see the usefulness of delegates here.

    So, in general you can say, delegates are used to achieve one or other kind of Observer Pattern.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Thursday, January 17, 2013 7:58 AM