Delegates
Sun Oct 12 2025 17:22:45 GMT+0000 (Coordinated Universal Time)
Saved by
@final
using System;
using System.Threading;
class Program
{
public delegate int MathOperation(int a, int b);
public static int Add(int a, int b)
{
Thread.Sleep(2000);
return a + b;
}
static void Main(string[] args)
{
Console.WriteLine("Main Started");
MathOperation add = Add;
Console.WriteLine("\nSynchronous call:");
int syncResult = add(5, 3);
Console.WriteLine($"Synchronous result: {syncResult}");
Console.WriteLine("\nAsync call using BeginInvoke:");
IAsyncResult asyncResult = add.BeginInvoke(20, 20, null, null);
Console.WriteLine("Main thread continues to run...");
int asyncResultValue = add.EndInvoke(asyncResult);
Console.WriteLine($"Asynchronous result: {asyncResultValue}");
Console.WriteLine("Main Ended");
}
}
content_copyCOPY
Comments