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.
When you want to create a delegate in C# you use the delegate type.
A Delegate is a type-safe object that can point to another method or possibly multiple methods in the application that can be invoked at a later time. Declaring a delegate is just like declaring a method, but it works as a Class type.
Syntax for delegate declaration:
delegate <return type> <delegate-name> <parameter list>
See in the figure:
Delegates have the capability to invoke more than one method at the same time.
Delegate are especially used for implementing events and call-back methods. All delegates are implicitly derived from System. Delegates class System. delegates is an abstract class. It's an OOP feature that we cannot create an instance of an abstract class. Therefore to make an instance of system.delegates we need to inherit it from a class.
See in the figure:
Instantiating Delegates
A delegate object must be created with the new keyword and associated with a specific method. When creating delegates, the argument passed to the new expression is written like a method call.
The following example describe the declaration, instantiation and use of a delegate:
- using System;
-
-
- namespace delegatetest
- {
-
- class Program
- {
- delegate void stringprint(string str);
- public static void stringmethod(string s)
- {
- Console.WriteLine("heloo stringmethod");
-
- }
-
- static void Main(string[] args)
- {
- stringprint printer = new stringprint(stringmethod);
- printer(" helo word");
- Console.ReadKey();
- }
-
- }
- }
Console Output
Note
We can declare a delegate type inside or outside the class because a delegate is a class type.
In the preceding example, I just declared a delegate type inside the class.
The following example describes the declaration, instantiation and use of a delegate that can be used to reference a method that takes an integer parameter and returns an integer value.
- using System;
- namespace delegatsretun
- {
- delegate int manipulatenumber(int n);
-
- class Program
- {
- static int num = 10;
-
- public static int addnumber(int p)
- {
-
- num = num + p;
- return num;
-
- }
- public static int mult(int q)
- {
- num = num * q;
- return num;
- }
-
- public static int getresult()
- {
- return num;
-
- }
- static void Main(string[] args)
- {
- Console.WriteLine("enter two values for add and multiple");
- int l =Convert .ToInt32( Console.ReadLine());
- int x = Convert.ToInt32(Console.ReadLine());
- manipulatenumber mp = new manipulatenumber(addnumber);
- mp(l);
- Console.WriteLine("value of addnumber {0}", getresult());
- manipulatenumber mps = new manipulatenumber(mult);
- mps(x);
- Console.WriteLine("value of mult {0}", getresult());
- Console.ReadKey();
- }
- }
- }
Console Output
Multicasting of a Delegate
Multicast delegates can be used to invoke multiple methods. The delegates instance can do multicasting and add a new method on the existing delegates instance using the "+=" operator and "-="operators can be used to add and remove (respectively) a method from a delegates instance. The following program describes the multicasting of delegates.
- using System;
-
- namespace multicast
- {
- delegate int manipulatenumber(int n);
-
-
- class Program
- {
- static int num = 10;
-
- public static int addnumber(int p)
- {
-
- num = num + p;
- return num;
-
- }
- public static int mult(int q)
- {
- num = num * q;
- return num;
- }
-
- public static int getresult()
- {
- return num;
-
- }
- static void Main(string[] args)
- {
- manipulatenumber md;
- manipulatenumber md1 = new manipulatenumber(addnumber);
- manipulatenumber md2 = new manipulatenumber(mult);
- md = md1;
- md += md2;
-
- md(20);
-
- Console.WriteLine("value of num {0}", getresult());
- Console.ReadKey();
-
-
- }
- }
- }
Console Output
Summary
In this article we came across what the basics of delegates actually are. I tried to put light on them with examples.