Delegates are different from normal methods in that they allow you to call them without knowing which method you are calling at compile time. They don't do anything that
can't be done with normal method calls, but they do allow code to be simpler and cleaner:
enum function
{
callPrintScreen,
callPrintPrinter,
callPrintPDF
}
private function func;
...
func = function.callPrintPDF;
...
switch (func)
{
case function.callPrintScreen:
PrintScreen(myString);
break;
case function.callPrintPrinter:
PrintPrinter(myString);
break;
case function.callPrintPDF:
PrintPDF(myString);
break;
}
Becomes:
private delegate void DoFunc(string s);
private DoFunc func;
...
func = PrintPDF;
...
func(myString);
If you need to add a new option (say PrintConsole), then you don't have to change the code which uses it at all - just the code that loads the delegate.