3

According to the MSDN documentation, it says it's useful for following purposes in addition to others which are understandable:

  1. A class may need more than one implementation of the method.
  2. It is desirable to encapsulate a static method.

    Can someone help me understand these usages with an example?

Learner
  • 980
  • 7
  • 20
  • 37

1 Answers1

4

A delegate is a reference to a method that you can pass around as an object.

Imagine how useful it could be to have a method that allows its callers to provide part of its own logic. Every caller can have their own method, create a delegate (reference) to their method, and pass it to the method as a parameter. As long as the main method knows what arguments to pass in (if any), it can invoke the method by its reference (delegate).

Here's a simple example, specifically of usage #1 in your question:

void RemoveItem(string item, Action preRemoveLogic)
{
   preRemoveLogic(); //we don't know what method this actually points to,
                     //but we can still call it.
   //remove the item
}

void MyCustomLogic()
{
   //do something cool
}

/* snip */
RemoveItem("the item", new Action(MyCustomLogic));
//I can pass a reference to a method! Neat!

Delegates are also very important for making events work in .NET.

Community
  • 1
  • 1
Rex M
  • 142,167
  • 33
  • 283
  • 313