why use delegates c# code example
Example 1: delegates in c#
using System;
using System.IO;
class Delegate
{
public delegate void HelloMethod(string str);
public static void Main(String [] args)
{
HelloMethod delegateInstance = new HelloMethod(print);
delegateInstance("Hey there! Iam delegate method");
}
public static void print(string delegateStr)
{
Console.WriteLine(delegateStr);
}
}
Example 2: what are delegates and how to use them c#
public delegate void MyDelegate(string text);
Example 3: what are delegates and how to use them c#
MyDelegate d = new MyDelegate(ShowText);
Example 4: what are delegates and how to use them c#
myDelegate d1 = new myDelegate(Method1);myDelegate d2 = new myDelegate(Method2);myDelegate multicastDelegate = (myDelegate)Delegate.Combine(d1, d2);multicastDelegate.Invoke();