Users Online

· Guests Online: 38

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C# Program to Display Results using Delegates

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.

 

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

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.66 seconds
10,844,163 unique visits