using System;
namespace practice
{
public delegate int MathOperation(int x, int y); // Step 1: Declare delegate
public class DelegateExample
{
public static int Add(int x, int y) // Step 2: Define a matching method
{
return x + y;
}
public static void Main()
{
MathOperation d = Add; // Step 3: Assign method to delegate
int res = d(2, 3); // Step 4: Invoke delegate
Console.WriteLine("Result: " + res);
}
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter