简体   繁体   中英

Understand specific usage of delegate in C#

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM