Generics is one of the most important concept of object oriented programming. It exists not only in C# but is also used extensively in popular programming languages including Java. While Generics is an important concept which is often neglected, it remains to be one of the favorite question in C# and .NET interviews.

One of our known was once working with TCS and on his very first viva during his training program, the mentor asked a basic .NET interview question on “Generics” which he failed to answer, well what so say on that! So one can’t ignore the importance of Generics, we guess.

What are Generics?

Generics was introduced as a concept in C# 2.0 along with Common Language Runtime (CLR). It is one of the strongest concept of .NET framework which allows us to separate the logic from datatype. This allows users to have high level of reusability and maintainability.

For instance, if I would like to declare a function ‘compare‘ which takes in two parameters of same type and returns true if value of parameters is equal then the function would be like:

public bool compare (int a, int b)
 {
 if (a.Equals(b))
 return true;
 else
 return false;
 }

Now if I need to define a similar function which does same operation of compare but for different datatype say string then the definition would be like:

public bool compare (string a, string b)
 {
 if (a.Equals(b))
 return true;
 else
 return false;
 }
 }

We can now clearly see that in above two functions, only difference is the datatype. Generics allows us to give a common definition for such functions.

We will use keyword UNKNOWNDATATYPE instead of actual datatypes and this will allow us to compare object of two similar kind on run-time. For instance, have a look at example below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Generics
{
 class Program
 {
 public static void Main(string[] args)
 {

 Console.WriteLine("ChewCode.com - Generics in C# Example");
 //ChewCode.com :Creating object of the class
 forComparison<int> intComp = new forComparison<int>();
 //ChewCode.com : We provided int above while creating object of class

 bool result = false;
 result = intComp.compare(22, 22);
 if (result)
 Console.WriteLine("ChewCode.com -Int Equal");
 else
 Console.WriteLine("ChewCode.com -Int Unequal");

 //ChewCode.com :Creating object of the class
 forComparison<string> strComp = new forComparison<string>();
 //ChewCode.com : We provided string above while creating object of class

 bool result1 = false;
 result1 = strComp.compare("chew", "code");
 if (result1)
 Console.WriteLine("ChewCode.com -String Equal");
 else
 Console.WriteLine("ChewCode.com -String Unequal");

 Console.ReadLine();

 }

 
 }

 public class forComparison<UNKNOWNDATATYPE>
 {
 public bool compare(UNKNOWNDATATYPE a, UNKNOWNDATATYPE b)
 {
 if (a.Equals(b))
 return true;
 else
 return false;
 }
 }
}

When we run the above program, the output is:

Generics-C-Sharp-and-DOT-NET-Interview-Questionzz

So we saw that how generics can ease our life and it proves out to be really handy in terms of significance and writing managed code.



ABOUT THE AUTHOR:

Software Developer by profession, Ankit is a Microsoft Certified Professional and beholds certification for 'Microsoft Specialist: Programming in C#'. Follow him at Facebook | Twitter | Google + | Android


Tags: , , , , , , , , , ,

Leave A Response