Delegates in C#
2.The signature of a delegate must match to the signature of the function the delegate points to, otherwise it will give compile time error. This is the reason, why delegates are type safe function pointer.
3.A delegate is similar to a class. You can create an instance of it, and pass the function as parameter in delegate constructor. This way, the delegate will point to that function.
Example:
public delegate void MyDelegate(string msg);
class Program
{
static void Main(string[] args)
{
MyDelegate del = new MyDelegate(PrintHello);
del("My delegate");
Console.Read();
}
public static void PrintHello(string message)
{
Console.WriteLine(message);
}
}
Good
ReplyDeleteGood
ReplyDelete