Delegating actions
There will be times when you need to pass off, or delegate, the execution of a method from one file to another. In C#, this can be accomplished through delegate types, which store references to methods and can be treated like any other variable. The only caveat is that the delegate itself and any assigned method need to have the same signature—just like integer variables can only hold whole numbers and strings can only hold text.
Creating a delegate is a mix between writing a function and declaring a variable:
public delegate returnType DelegateName(int param1, string param2);
You start with an access modifier followed by the delegate
keyword, which identifies it to the compiler as a delegate
type. A delegate
type can have a return type and name as a regular function, as well as parameters if needed. However, this syntax only declares the delegate
type itself; to use it, you need to create an instance as we do with classes:
public DelegateName...