Function pointers are not safe. In thousand lines of code, if we have to invoke the function using function pointer, we cannot assure that the pointer points to function without going through the code.
We cannot ensure the number of parameters to the function or the order of parameters. Order of parameters refer to data types. What will happen if we invoked a function with function pointer & pointer is not pointing to function? What will happen if we invoked a function with function pointer & number of arguments are more or less passed? What will happen if we invoked a function with function pointer & order of parameters is wrong? (e.g. Function accepts int first & we pass string first.)
In all cases, our application will crash. In essence, Function pointers are not type-safe.
.NET has got a concept of delegates which are type-safe function pointers with ability to point to multiple functions. Delegates are declared in C# using keyword delegate.
Declaration of delegate is just like function declaration. That is, the delegate must be declared with return data type & parameters. Parameter names are not relevant.
Internally, a class "DisplayDelegate" is generated which derives from System.MulticastDelegate. System.MulticastDelegate derives from System.Delegate. System.Delegate is the base class. One can create the object of "DisplayDelegate" class. The constructor accepts the function which has same signature as that of delegate declaration.
To invoke function using delegate, use Invoke function on delegate object with parameter values for the function. The function wrapped in delegate can be static or instance function.
Let's see, how delegates are type-safe? When one creates a delegate object, the delegate is aware of the parameters required. Wherever you call the function using delegate, compiler will not compile the program if,
- function specified in constructor is not with appropriate function signature.
- the number of parameters are less or more while invoking the function.
- the order of parameters is not appropriate.
System.Delegate class has two static methods Combine and Remove. Use the methods to provide the delegate with multiple functions.
System.Delegate has two properties Method and Target. The derived class have these properties inherited. "Method" property gets the method represented by delegate. "Target" gets the instance/object on which the delegate operates. "Target" returns null for static methods.
If delegate has multiple functions, one can use GetInvocationList method of System.Delegate class to retrieve System.Delegate array.