Learn use of Delegates in C# programming
Hope you must have gone through my previous articles Class members, Inheritance & Interface, and Generics. Now, we’ll discuss on Delegates. A delegate defines a signature method and can create a reference to any method with a well-matched signature. You can call the method only through the delegate. Delegates help pass methods as point a of view to other methods for the purpose of signature validation between two methods.
Please note that Event handlers are similar to methods that are called through delegates. The Event handling task is very much important for a programming aspect. Hence, delegates are broadly used in event handling.
Please note that Event handlers are similar to methods that are called through delegates. The Event handling task is very much important for a programming aspect. Hence, delegates are broadly used in event handling.
Codes to create a delegate:
public delegate void TCDelegate(string str);
Codes to create a reference to a method, which perfectly matches the signature mentioned by the delegate:
class SampleClass { // Method that matches the TCDelegate signature. public static void TCMethod(string message) { // Add code here. } // Method that instantiates the delegate. void TCDelegate() { TCDelegate sd = tcMethod; sd("Sample string"); } }
Overall, Delegates are the vital signature methods for the validation of signatures between two methods. The above code must help you in using delegate in an error-free manner.
Post A Comment:
0 comments: