// Class method will be notified with the delegate.
static class MyClass
{
// Create a static method that we plan to report to the delegate.
public static void Method()
{
Console.WriteLine("Line brought this method with a delegate.");
}
}
// 21-th line create a class-delegate named MyDelegate,
// method that will be notified with an instance of this class delegate
// will take anything and there will be nothing to return.
public delegate void MyDelegate(); // Create a delegate class. (1)
class Program
{
static void Main()
{
MyDelegate myDelegate = new MyDelegate(MyClass.Method); // Create an instance of the delegate. (2)
myDelegate.Invoke(); // Call this method with a delegate. (3)
myDelegate(); // Another way to call this method with a delegate. (3')
// Delay.
Console.ReadKey();
}
}
public delegate void MyDelegate(); // Create a delegate class. (1)
public static void Method()
{
Console.WriteLine("Line brought this method with a delegate.");
}
public delegate void MyDelegate();
), you specify the signature of methods which can then be reported by the delegate.public delegate int MyDelegate(int a, int b);
public static int Sum(int a, int b)
{
return a+b;
}
class Program
{
static void Main()
{
MyDelegate myDelegate = new MyDelegate(MyClass.Sum);
int result = myDelegate(5, 10);
}
}
MyDelegate myDelegate = new MyDelegate(MyClass.Method)
Find more questions by tags C#