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:
- using System;
- using System.Collections.Generic;
-
- namespace generic
- {
- class Program
- {
-
- static void swap<t>(ref t p, ref t q)
- {
- t temp;
- temp = p;
- p = q;
- q = temp;
-
- }
- static void Main(string[] args)
- {
- int a, b;
- char c, d;
- string str1, str2;
- a = 5;
- b = 10;
- c = 'a';
- d = 'c';
- str1 = "hello";
- str2 = "frnd";
-
- Console.WriteLine("interger values before swap");
- Console.WriteLine("a={0},b={1}", a, b);
- Console.WriteLine("char values before swap");
- Console.WriteLine("c={0},d={1}", c, d);
- Console.WriteLine("string values before swap");
- Console.WriteLine("str1={0},str2={1}", str1, str2);
-
- swap<int>(ref a, ref b);
- swap<char>(ref c, ref d);
- swap<string>(ref str1, ref str2);
-
-
- Console.WriteLine("interger values after swap");
- Console.WriteLine("a={0},b={1}", a, b);
- Console.WriteLine("char values after swap");
- Console.WriteLine("c={0},d={1}", c, d);
- Console.WriteLine("string values after swap");
- Console.WriteLine("str1={0},str2={1}", str1, str2);
- Console.ReadKey();
- }
- }
- }
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.
- using System;
- using System.Collections.Generic;
- namespace ConsoleApplication2
- {
- delegate T numbermanipulate<T>(T num);
- class Program
- {
- static int number = 10;
- public static int addnumber(int n)
- {
- number = number + n;
- return number;
-
-
- }
- public static int multinumber(int q)
- {
- number = number * q;
- return number;
-
- }
-
- public static int getvalues()
- {
-
- return number;
-
- }
-
- static void Main(string[] args)
- {
- Console.WriteLine("enter two values");
- int k = Convert.ToInt32(Console.ReadLine());
- int m = Convert.ToInt32(Console.ReadLine());
-
- numbermanipulate<int> nm = new numbermanipulate<int>(addnumber);
- nm(k);
- Console.WriteLine("addnumber values" + getvalues());
- numbermanipulate<int> nm1 = new numbermanipulate<int>(multinumber);
- nm1(m);
- Console.WriteLine("multi values" + getvalues());
- Console.ReadKey();
-
- }
- }
- }
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:
- using System;
- using System.Collections.Generic;
-
-
- namespace ConsoleApplication2
- {
- delegate string delegatsus<T1, T2>(T1 a, T2 b);
- class delegatediffrent
- {
- static string addnum(int a, int b)
- {
-
- return (a + b).ToString();
-
- }
- static string addfloat(double m, double n)
- {
-
- return (m + n).ToString();
-
- }
- static string addstring(string s1, string s2)
- {
- return (s1 + s2);
-
- }
- public static void Main()
- {
-
- delegatsus<int, int> getinterger = new delegatsus<int, int>(addnum);
-
- Console.WriteLine(getinterger(3, 3));
- delegatsus<double, double> getfloat = new delegatsus<double, double>(addfloat);
- Console.WriteLine(getfloat(5.5, 7.5));
- delegatsus<string, string> getstring = new delegatsus<string, string>(addstring);
- Console.WriteLine(getstring("hello", "frnd"));
- Console.ReadKey();
- }
- }
- }
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.