Information Technology Reference
In-Depth Information
The following code shows an example of a generic delegate. In Main , generic delegate
MyDelegate is instantiated with an argument of type string and initialized with method
PrintString .
delegate void MyDelegate<T>(T value); // Generic delegate
class Trivial
{
static public void PrintString(string s) // Method matches delegate
{
Console.WriteLine(s);
}
static public void PrintUpperString(string s) // Method matches delegate
{
Console.WriteLine("{0}", s.ToUpper());
}
}
class Program
{
static void Main()
{
MyDelegate<string> MyDel = // Create inst of delegate
new MyDelegate<string>(Trivial.PrintString);
MyDel += Trivial.PrintUpperString; // Add a method.
MyDel("Hi There."); // Call delegate
}
}
This code produces the following output:
Hi There.
HI THERE.
Search WWH ::




Custom Search