Posts

Showing posts with the label #Delegates

C# Delegates

Image
The delegate is a reference type data type that   defines the method signature . You can define variables of delegate, just like other data type, that can refer to any method with the same signature as the delegate. There are three steps involved while working with delegates: Declare a delegate Create an instance and reference a method Invoke a delegate A delegate can be declared using the  delegate  keyword followed by a function signature, as shown below. Delegate Syntax [access modifier] delegate [return type] [delegate name]([parameters]) The following declares a delegate named  MyDelegate . Example: Declare a Delegate public delegate void MyDelegate( string msg ); Above, we have declared a delegate  MyDelegate  with a  void  return type and a string parameter. A delegate can be declared outside of the class or inside the class. Practically, it should be declared out of the class. After declaring a delegate, we need to set the target ...