Delegates are a mechanism of indirectly calling methods at runtime. The primary use of delegates in C# programming is for implementing events and the call-back methods.
Important Points About Delegate
- Delegate is a strongly typed function pointer (just like pointer is there in C++ language).
- A Delegate is a reference type variable which holds the reference of a method.
- We can call delegate like methods in C#.
- Delegates are used to raise the event in class and associated with the event handler.
- Delegate can be Non-Generic/ Generic.
Types of Delegate
There are two types of delegate.
Note
Generics are a kind of parameterized data structure that can work with value type as well as reference type. Using generics we can define the data type at runtime using System.Collections.Generic namespace.
- Single cast Delegate
- Single cast Delegate: A single cast delegate is derived from System.Delagate Class.
- It contains reference of only one method at a time.
- Multi-cast Delegate
- Multi-cast Delegate: Multicast Delegate is derived from System.MulticatsDelagate class.
- It Creates an invocation list of multiple methods.
- In Multicast delegate we create a single delegate that invokes multiple methods.
- It holds the reference of multiple methods and it executes all the methods as in calling order.
- The multiple method called by delegate should not return any values.
Syntax
< public delegate return Type(void/string) delegate_Name (); >
Or
< public delegate return Type(void/string) delegate_Name <T> (); >
Example
Single Cast Non-Generic/Generic Delegate,
- namespace Demo_Delegate
- {
-
- class Single_Cast
- {
-
-
- public delegate void Single_Cast_delegate();
-
-
- private static void message()
- {
- Console.WriteLine("This Example of Single Cast Non-generic Delegate");
- }
- static void Main(string[] args)
- {
-
- Single_Cast_delegate SCD = new Single_Cast_delegate(message);
-
-
- Single_Cast_Generic SCG = new Single_Cast_Generic();
- Mydel<int> my1 = new Mydel<int>(SCG.add);
- Mydel<string> my2 = new Mydel<string>(SCG.Myname);
-
-
- SCD();
- my1(10, 20);
- my2("My", "Name");
- }
-
- }
-
-
- public delegate void Mydel<T>(T a, T b);
- class Single_Cast_Generic
- {
- public void add(int a, int b)
- {
- int Sum = a + b;
- Console.WriteLine(Sum);
-
- }
-
- public void Myname(string firstname, string lastname)
- {
- string fn = firstname + " " + lastname;
- Console.WriteLine(fn);
-
- }
- }
- }
Multicast Non-Generic/Generic Delegate,
- namespace Demo_Delegate
- {
- class MyClass
- {
- static void Main(string[] args)
- {
-
- MultiCast_delegate md = new MultiCast_delegate();
- NonGenericMulticaste NGM = new NonGenericMulticaste(md.FirstMessage);
-
-
- NGM += new NonGenericMulticaste(md.SecondMessage);
- NGM("Call Mutliple methods using single Object");
-
-
-
- MultiCast_Genericdelegate MG = new MultiCast_Genericdelegate();
-
-
- GenericMulticaste<int> obj1 = new GenericMulticaste<int>(MG.FirstMessage);
- obj1 += new GenericMulticaste<int>(MG.Message);
-
- GenericMulticaste<string> obj2 = new GenericMulticaste<string>(MG.SecondMessage);
- obj2 += new GenericMulticaste<string>(MG.SecondMessage);
-
- obj1(100);
-
- obj2("Call Mutliple Generic methods using single Object");
-
-
-
- }
- }
-
- public delegate void NonGenericMulticaste(string Message);
- class MultiCast_delegate
- {
- public void FirstMessage(string Message1)
- {
- Console.WriteLine(Message1);
- }
- public void SecondMessage(string Message2)
- {
- Console.WriteLine(Message2);
- }
- }
-
- public delegate void GenericMulticaste<T>(T Message);
- class MultiCast_Genericdelegate
- {
- public void FirstMessage(int Number)
- {
- Console.WriteLine("Your Number is:"+Number);
- }
- public void Message(int Number)
- {
- Console.WriteLine("Your Number is:" + Number);
- }
- public void SecondMessage(string Generic_Message)
- {
- Console.WriteLine(Generic_Message);
- }
- public void Second(string Generic_Message)
- {
- Console.WriteLine(Generic_Message);
- }
- }
- }
Following are the predefined generic delegates,
- public delegate TResult Func<in T, out TResult>(T arg);
- public delegate void Action()
- public delegate bool Predicate<in T>(T obj);
Example
- namespace PreDefinedGenericDelegates
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- Action<int,int> a = new Action<int,int>(MyClass.sum);
- a(5, 6);
- Action<string, string> b = new Action<string, string>(MyClass.add);
- b("Kundan", "Jha");
-
-
-
- Func<int, int, int> ds = new Func<int, int, int>(MyClass.Multi);
- Console.WriteLine(ds(45, 65));
-
-
- Predicate<string> p = new Predicate<string>(MyClass.IsDate);
- if (p("01-01-2015"))
- {
- Console.WriteLine("Valid");
- }
- else
- {
- Console.WriteLine("Invalid");
- }
-
- Console.ReadKey();
-
- }
- }
-
- class MyClass
- {
- public static bool IsDate(string date)
- {
- DateTime dt;
- return DateTime.TryParse(date, out dt);
- }
-
- public static int Multi(int a, int b)
- {
- return a + b;
- }
- public static void sum(int num1, int num2)
- {
- int sum = num1 + num2;
- Console.WriteLine(sum);
-
- }
- public static void add(string fname, string lname)
- {
- string fullname = fname + " " + lname;
- Console.WriteLine(fullname);
- }
- }
- }
I hope you liked this article. Thank You.