What is delegate?
A delegate is a type-safe function pointer that holds the reference to a function. The signature of the delegate must match the signature of the function that it points to, else you will get a compilation error. Delegate is type-safe because it points to a function and holds the signature of the function.
How to declare delegates in C#?
A delegate is similar to a class. We can create an instance of a delegate, however, while creating the instance, we have to pass the function name as a parameter to the delegate constructor. All delegates are implicitly derived from the System.Delegate class.
Syntax
Delegate<return type><delegate name><parameter list>
- Delegate void HelloWorld(string message)
What are the different types of Delegates?
Single Delegate
The delegate that can call a single method is called a single delegate.
- using System;
- namespace Delegate_Demo
- {
- public delegate void Message(string msg);
- class Program
- {
- static void Main(string[] args)
- {
- Message msg = new Message(PrintMessage);
- msg("Welcome to delegate");
- Console.ReadLine();
- }
- public static void PrintMessage(string message)
- {
- Console.WriteLine(message);
- }
- }
- }
Multicast Delegate
A delegate which calls multiple methods is called Multicast Delegate. The + (plus) and – (minus) operators are used to subscribe and unsubscribe. A Multicast delegate is a delegate that has references to more than one functions. When you invoke a multicast delegate, all the functions the delegate is pointing to, are invoked.
- using System;
- namespace Delegate_Demo
- {
- class MulticastDelegate
- {
- public delegate void PrintMessage();
- static void Main()
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- PrintMessage msg = new PrintMessage(Greeting);
- msg += Wish;
- msg -= Suggesion;
- msg();
- Console.ReadLine();
- }
-
- public static void Greeting()
- {
- Console.WriteLine("Good Morning");
- }
- public static void Wish()
- {
- Console.WriteLine("happy Wedding Anniversary");
- }
- public static void Suggesion()
- {
- Console.WriteLine("You should take your wife for a tour.");
- }
- }
- }
Generic Delegate
Generic delegate does not require an instance to be defined.
What are the types of generic delegates?
There are three types of generic delegates
Action is a delegate (pointer) to a method, that takes zero, one, or more input parameters, but does not return anything.
- using System;
- namespace Delegate_Demo
- {
- class ActionDelegate
- {
- static void Main()
- {
-
-
-
-
-
-
- Action<int> yourAge = new Action<int>(Age);
- yourAge(30);
- Console.ReadLine();
- }
-
- public static void Age(int age)
- {
- Console.WriteLine("Your Age is:"+ age);
- }
- }
- }
Func is a delegate (pointer) to a method, that takes zero, one, or more input parameters, and returns a value (or reference).
- using System;
- namespace Delegate_Demo
- {
- public delegate int Marks(int a,int b,int c,int d, int e, int f);
- class FunDelegate
- {
- static void Main()
- {
- Marks marks = CalculateMarks;
- int Result = marks(80, 75, 58, 72, 89, 60);
- Console.WriteLine("Total Marks:" + Result);
- Console.ReadLine();
- }
-
- public static int CalculateMarks(int hindi,int english, int physics, int chemistery, int biology, int math)
- {
- return hindi + english + physics + chemistery + biology + math;
- }
- }
- }
Predicate is a special kind of Func often used for comparisons.
- using System;
-
- namespace Delegate_Demo
- {
- class Predicate
- {
- static void Main()
- {
- Predicate<int> voter = IsEligibleForVote;
- bool result=voter(18);
- Console.WriteLine("Eligible for voting: " + result);
- Console.ReadLine();
- }
-
- public static bool IsEligibleForVote(int age)
- {
- if (age >= 18)
- return true;
- else
- return false;
- }
- }
- }
We use delegates when -
- An event design pattern is used.
- It is desirable to encapsulate a static method.
- The caller has no need to access other properties, methods, or interfaces on the object implementing the method.
- An easy composition is desired.
- A class may need more than one implementations of the method.