locked
What is the use of delegates? why can't we just call the method directly, instead of calling it via a delegate? RRS feed

  • Question

  • User-1792387899 posted

    What is the use of delegates? why can't we just call the method directly, instead of calling it via a delegate?

    Thanks in advance

    Friday, May 23, 2014 4:09 AM

Answers

  • User-821857111 posted

    Nikita Deshmukh

    why can't we just call the method directly, instead of calling it via a delegate?

    Because the method doesn't exist. The method body is passed in as a parameter to the delegate instead.

    See if this helps you understand more easily: http://www.mikesdotnetting.com/Article/91/Delegates-Anonymous-Methods-and-Lambda-Expressions-in-CSharp

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 23, 2014 4:53 AM
  • User741532998 posted

    It is your decision to use delegates or not, Just some times using delegates is easy and clean.

    like

    1.multiple method calls with one event trigger.

    2.Reduce the Dependency on your code. (Class A is calling method in Class B directly, A depend on B), delegate will get rid this dependency

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 23, 2014 5:38 PM

All replies

  • User-821857111 posted

    Nikita Deshmukh

    why can't we just call the method directly, instead of calling it via a delegate?

    Because the method doesn't exist. The method body is passed in as a parameter to the delegate instead.

    See if this helps you understand more easily: http://www.mikesdotnetting.com/Article/91/Delegates-Anonymous-Methods-and-Lambda-Expressions-in-CSharp

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 23, 2014 4:53 AM
  • User741532998 posted

    It is your decision to use delegates or not, Just some times using delegates is easy and clean.

    like

    1.multiple method calls with one event trigger.

    2.Reduce the Dependency on your code. (Class A is calling method in Class B directly, A depend on B), delegate will get rid this dependency

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 23, 2014 5:38 PM