Home > DeveloperSection > Forums > Delegates in C#
Ankit Singh

Total Post:341

Posted on    December-21-2015 9:58 PM

 C# C#  .NET 

 1 Reply(s)
 296  View(s)
Rate this:
I want to use Delegates in c# please help me how to use this.

aditya kumar Patel

Total Post:254

Posted on    December-21-2015 9:59 PM

C# delegates are similar to pointers to functions, in C or C++. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.

Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegate class.

using System;

delegate int ReplaceNumber(int n);
namespace DelegateApp
   class UseDelegate
      static int number = 10;
      public static int AddNumber(int p)
         number += p;
         return number;

      public static int MultNumber(int q)
         number *= q;
         return number;
      public static int getNumber()
         return number;

      static void Main(string[] args)
         //create delegate instances
         ReplaceNumber numberc1 = new ReplaceNumber(AddNumber);
         ReplaceNumber numberc2 = new ReplaceNumber(MultNumber);
         //calling the methods using the delegate objects
         Console.WriteLine("First Value of Number: {0}", getNumber());
         Console.WriteLine("Second Value of Number: {0}", getNumber());

Don't want to miss updates? Please click the below button!

Follow MindStick