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.
Codes to create a delegate:
1 public delegate void TCDelegate(string str);
Codes to create a reference to a method, which perfectly matches the signature mentioned by the delegate:
1 class SampleClass
3 // Method that matches the TCDelegate signature.
4 public static void TCMethod(string message)
6 // Add code here.
9 // Method that instantiates the delegate.
10 void TCDelegate()
12 TCDelegate sd = tcMethod;
13 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.
Top comments (0)