C# Program to Display Results using Delegates

This is a C# Program to display results using delegates.

Problem Description

This C# Program Displays Results using Delegates.

Problem Solution

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.

Program/Source Code

Here is 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.

/*
 *  C# Program to Display Results using Delegates
 */
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;
    }
}
Program Explanation

This C# program is used to display results using delegates. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime. Using result variable compute the sum() function by passing 50 and 20 values as an argument.

advertisement
advertisement

The sum() function is used to compute the summation of two values, 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. Print the result using delegates.

Runtime Test Cases
 
Result is: 70

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.