Write a C# Program to Display Results Using Delegates

Write a C# Program to Display Results Using Delegates:

C# Program Displays Results using Delegates. Here Delegate is a type which holds the method(s) reference in an object. It is also referred to as a type safe function pointer.

Source code of the C# Program to Display Results using Delegates. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

ANS:

using System;
public class example
{
    public delegate int DelegateHandler(int a, int b);
    static void Main(string[] args)
    {
        Results Results = new Results();
        DelegateHandler sum = new DelegateHandler(Results.sum);
        int result = sum(50, 20);
        Console.WriteLine("Result is: " + result);
        Console.ReadLine();
    }
}

public class Results
{
    public int sum(int a, int b)
    {
        return a + b;
    }
}

Output:

Result is: 70.

Post a Comment

Previous Post Next Post