In C#, you can store a delegate function using the Delegate
class which is a base class for all delegate types. To present a confirmation message before executing the stored action, you can create a new delegate that wraps the original delegate with a lambda expression that first displays a message to the user and then calls the original delegate if the user confirms.
Here's an example:
main.cs869 chars37 lines
In this example, we define a new delegate type called ActionDelegate
that represents a function that takes no arguments and returns no value. We then create a new instance of this delegate type called originalAction
that wraps the DoSomething()
method.
Next, we create a new delegate instance called confirmedAction
using a lambda expression. This delegate presents a confirmation message and then calls the originalAction
delegate if the user confirms.
Finally, we pass the confirmedAction
delegate to the RunAction()
method which executes the wrapped action.
gistlibby LogSnag