use of delegates in c# code example

Example 1: delegate function c#

// Create the Delgate method.
public delegate void Del(string message);

// Create a method for a delgate.
public static void DelegateMethod(string message)
{
  Console.WriteLine(message);
}

// Instatiate the delegate.
Del hadler = DelegateMethod;

// Call the delegate.
hadler("Hello World");

// Output
// Hello World

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#

delegate result-type identifier ([parameters])

Example 5: 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();