Generic Delegates in C#

In this article I am trying to shed some light on Generic Delegates in C#If you are reading this article then you must be familiar with the basics of delegates in C#. If you do not understand delegates then I recommend you go through my previous article.

Before getting into Generic Delegates.

What is  Generic

I want to give a short overview of Generic.

Generic is a type to be specified later. They are instantiated when needed for a specific type provided as parameters. In other words, a Generic allows you to write a class or method that can work with any data type.

Let us see in a figure:


Features of generics

  • It helps you to maximize code reuse type safety and performance.

  • The System.Collection.Generic namespace contains several new generic collection classes. You need to use the generic collection classes instead of the collection classes in the System.Collections namespace.

  • You can create user-defined generic interfaces, classes, methods, events and delegates.

  • A common use of Generics is to create collection classes.

Let us see anohter example:

  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. namespace generic  
  5. {  
  6.     class Program  
  7.     {  
  8.   
  9.         static void swap<t>(ref t p, ref t q) // generic methods  
  10.         {  
  11.             t temp;  
  12.             temp = p;  
  13.             p = q;  
  14.             q = temp;  
  15.   
  16.         }  
  17.         static void Main(string[] args)  
  18.         {  
  19.             int a, b;  
  20.             char c, d;  
  21.             string str1, str2;  
  22.             a = 5;  
  23.             b = 10;  
  24.             c = 'a';  
  25.             d = 'c';  
  26.             str1 = "hello";  
  27.             str2 = "frnd";  
  28.             //display values before swap;  
  29.             Console.WriteLine("interger values before swap");  
  30.             Console.WriteLine("a={0},b={1}", a, b);  
  31.             Console.WriteLine("char values before swap");  
  32.             Console.WriteLine("c={0},d={1}", c, d);  
  33.             Console.WriteLine("string values before swap");  
  34.             Console.WriteLine("str1={0},str2={1}", str1, str2);  
  35.             //call swap;  
  36.             swap<int>(ref a, ref b);  
  37.             swap<char>(ref c, ref d);  
  38.             swap<string>(ref str1, ref str2);  
  39.             //display values after swap;  
  40.   
  41.             Console.WriteLine("interger values after swap");  
  42.             Console.WriteLine("a={0},b={1}", a, b);  
  43.             Console.WriteLine("char values after swap");  
  44.             Console.WriteLine("c={0},d={1}", c, d);  
  45.             Console.WriteLine("string values after swap");  
  46.             Console.WriteLine("str1={0},str2={1}", str1, str2);  
  47.             Console.ReadKey();  
  48.         }  
  49.     }  
  50. }  
Console Output


   
Generics Delegates

The following is the syntax of Generic delegates.

You can define a Generic delegate with type parameters.

delegate T numbermanipulat<T>(T var);

T is the generic type parameter that allows you to specify an arbitrary type (T) to a method at compile time without specifying a collection type in the method or class declaration.

In the following example, I am taking two methods with the same type parameters and the same return type.

The following example shows the use of generic delegates.

  1. using System;  
  2. using System.Collections.Generic;  
  3. namespace ConsoleApplication2  
  4. {  
  5.     delegate T numbermanipulate<T>(T num);  
  6.     class Program  
  7.     {  
  8.         static int number = 10;  
  9.         public static int addnumber(int n)  
  10.         {  
  11.             number = number + n;  
  12.             return number;  
  13.   
  14.   
  15.         }  
  16.         public static int multinumber(int q)  
  17.         {  
  18.             number = number * q;  
  19.             return number;  
  20.   
  21.         }  
  22.   
  23.         public static int getvalues()  
  24.         {  
  25.   
  26.             return number;  
  27.   
  28.         }  
  29.   
  30.         static void Main(string[] args)  
  31.         {  
  32.             Console.WriteLine("enter two values");  
  33.             int k = Convert.ToInt32(Console.ReadLine());  
  34.             int m = Convert.ToInt32(Console.ReadLine());  
  35.             //create delegates instance  
  36.             numbermanipulate<int> nm = new numbermanipulate<int>(addnumber);  
  37.             nm(k);  
  38.             Console.WriteLine("addnumber values" + getvalues());  
  39.             numbermanipulate<int> nm1 = new numbermanipulate<int>(multinumber);  
  40.             nm1(m);  
  41.             Console.WriteLine("multi values" + getvalues());  
  42.             Console.ReadKey();  
  43.   
  44.         }  
  45.     }  
  46. }  
Output

 

Now in the following example, I am taking three methods with different parameters and the same return type.

Declaration

delegate string delegatsus<T1,T2>(T1 a,T2 b);

Let us see the example:

  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4.   
  5. namespace ConsoleApplication2  
  6. {  
  7.     delegate string delegatsus<T1, T2>(T1 a, T2 b);  
  8.     class delegatediffrent  
  9.     {  
  10.         static string addnum(int a, int b)  
  11.         {  
  12.   
  13.             return (a + b).ToString();  
  14.   
  15.         }  
  16.         static string addfloat(double m, double n)  
  17.         {  
  18.   
  19.             return (m + n).ToString();  
  20.   
  21.         }  
  22.         static string addstring(string s1, string s2)  
  23.         {  
  24.             return (s1 + s2);  
  25.   
  26.         }  
  27.         public static void Main()  
  28.         {  
  29.             // create delagte instance  
  30.             delegatsus<intint> getinterger = new delegatsus<intint>(addnum);  
  31.             //getinterger(30, 40);  
  32.             Console.WriteLine(getinterger(3, 3));  
  33.             delegatsus<doubledouble> getfloat = new delegatsus<doubledouble>(addfloat);  
  34.             Console.WriteLine(getfloat(5.5, 7.5));  
  35.             delegatsus<stringstring> getstring = new delegatsus<stringstring>(addstring);  
  36.             Console.WriteLine(getstring("hello""frnd"));  
  37.             Console.ReadKey();  
  38.         }  
  39.     }  
  40. }  

Output:

 

Summary

In this article we came across what generics actually are and what generic delegates are. I tried to shed some light on both with examples.


Similar Articles