Delegate Invocation Syntax

n C#, you typically invoke a delegate by using the variable referring to the delegate instance as if it were a method. For example, in the code below, the variable del1 is a delegate instance and you invoke it by using del1 as if it was a method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
static void Method1(string text)
{
    Console.WriteLine(text);
}
 
static void Main(string[] args)
{
    StringHandlerDelegate del1 = Method1;
 
    // Invoke the delegate instance
    del1("Invoked through delegate");
 
    Console.ReadLine();
}
Alternatively, you can call the Invoke method on the delegate instance.  This does exactly the same thing as the example above.
1
2
// Invocation, method #2
del1.Invoke("Also invoked thru delegate");
Previous
Next Post »