Delegates are used to hook up an event to a method that will handle that event (in fact, events themselves are just a restricted kind of delegate).
Using a delegate you can encapsulate any matching method that has the the same signature (same parameters) and return type.
They are ideal for:
- event handling: “Call this method when this event happens”;
- callbacks: “Call this method when you’re done doing this work”;
The signature of a single cast delegate is shown below:
public delegate int ButtonClickHandler (object obj1, object obj2)This declaration defines a delegate named ButtonClickHandler, which will encapsulate any method that takes two objects as parameters and returns an int.
Related articles:
- A great article with examples: Delegates and Events in C# / .NET
No comments:
Post a Comment