A Simple Way to Learn Generics

What are Generics?

Generics is a mechanism offered by the Common Language Runtime (CLR) and programming languages that provide one form of code re-use and algorithm re-use.

Advantages of Generics

  • Type Safe
  • Better Performance

Let us see how we can implement Generics in our code.

I’ve created a class MyGenericSample which has a method ShowMessage that takes two parameters of which one is a Known Type string argument and another being a generic type mentioned as T. Remember defining it in <> before you use.

  1. public void ShowMessage<T>(string msg, T value) //Passed two arguments where msg is Known Type and value as Generic Type  
  2. {  
  3.    Console.WriteLine("{0}: {1}", msg, value);  
  4. }  
I’ve overloaded ShowMessage method with three parameters of which one is a Known Type string argument and another two being generic types mentioned as Type1 and Type2.
  1. public void ShowMessage<Type1, Type2>(string msg, Type1 firstParameter, Type2 secondParameter) //Passed three arguments where msg is Known Type and Type1, Type2 as Generic Types  
  2. {  
  3.    Console.WriteLine("{0}: {1} {2}", msg, firstParameter, secondParameter);  
  4. }  
Now let’s see how they behave when they are called.
  1. static void Main(string[] args)  
  2. {  
  3.     MyGenericSample sample = new MyGenericSample();  
  4.     sample.ShowMessage < int > ("My Pincode", 500068); //Note: Declare the type of value we are going to send as Generic Type in <>  
  5.     sample.ShowMessage < string > ("My City""Hyderabad");  
  6.     sample.ShowMessage < char > ("Gender"'M');  
  7.     //Calling a Generic Method With Various Parameter Types  
  8.     sample.ShowMessage < stringstring > ("Full Name""Sridhar""Sharma");  
  9.     Console.ReadLine();  
  10. }  
Complete code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace GenericsSample  
  7. {  
  8.     public class MyGenericSample  
  9.     {  
  10.         public void ShowMessage < T > (string msg, T value) //Passed two arguments where msg is Known Type and value as Generic Type  
  11.             {  
  12.                 Console.WriteLine("{0}: {1}", msg, value);  
  13.             }  
  14.         public void ShowMessage < Type1, Type2 > (string msg, Type1 firstParameter, Type2 secondParameter) //Passed three arguments where msg is Known Type and Type1, Type2 as Generic Types  
  15.             {  
  16.                 Console.WriteLine("{0}: {1} {2}", msg, firstParameter, secondParameter);  
  17.             }  
  18.     }  
  19.     class Program  
  20.     {  
  21.         static void Main(string[] args)  
  22.         {  
  23.             MyGenericSample sample = new MyGenericSample();  
  24.             sample.ShowMessage < int > ("My Pincode", 500068); //Note: Declare the type of value we are going to send as Generic Type in <>  
  25.             sample.ShowMessage < string > ("My City""Hyderabad");  
  26.             sample.ShowMessage < char > ("Gender"'M');  
  27.             //Calling a Generic Method With Various Parameter Types  
  28.             sample.ShowMessage < stringstring > ("Full Name""Sridhar""Sharma");  
  29.             Console.ReadLine();  
  30.         }  
  31.     }  
  32. }  
Output

basics of generics

Conclusion

Generics makes the life of developers easy, since they offer Type Safe and there is a performance increase compared with non-generic types.

 

Up Next
    Ebook Download
    View all
    Learn
    View all