Introduction
This article is written to gain a clear understanding on Generics: why we use it, are there any workaround, why Generics are so important, and why we use this type-safe data structure rather than Standard Collections. This article is explained with basic examples of Generics for clear understanding.
Generics
To the people who aren't familiar with Generics in C#, Generics is simply a class that gives us access to create classes and methods with a placeholder.
So, what is a placeholder?
In programming, the meaning of placeholder is a simple character or a word or something like a string of characters that takes the position of final data temporarily.
Sounds good! Now why do we need a character to be a placeholder?
If you are aware, whenever you go through a complex program you might have seen a Character T inside Angle brackets <> just like this <T> and we ask ourselves a question, What does this <T> mean and why do we need it?
To answer this question, <T> is a simple character inside angle brackets that's telling us: "Hey coders! I’m just a generic type parameter and please put me beside a class so that I will be useful as a Type in the place of a datatype, in your program."
If you’re still wondering what the above statement meant:
It’s saying that, at present, you can use me in place of a data type, and as per your need you can replace me with my datatype friends like int, string, etc., in your program.
This example code will suffice to gain a clear understanding of Generics:
- using System;
- public class Hospital<T>
- {
- private T Cases;
- public Hospital(T value)
- {
- this.Cases=value;
- }
- public void Show()
- {
- Console.WriteLine(this.Cases);
- }
- }
-
- class Test
- {
- static void Main(string[] args)
- {
- Hospital<int> a = new Hospital<int>(100);
- Hospital <string> b = new Hospital<string("Hospital Cases");
- a.Show();
- b.Show();
- Console.ReadLine();
- }
- }
We can make our Hospital class work on two Generic Types.
For example,
- using System;
- class Hospital<T, U>
- {
- public T PatientCount
- {
- get;
- set;
- }
- public U Doctors
- {
- get;
- set;
- }
- }
-
- class Test
- {
- static void Main(string[] args)
- {
- Hospital<int, string> a = new Hospital<int, string>();
- a.PatientCount = 100;
- a.Doctors = "Available";
- Console.WriteLine(a.PatientCount);
- Console.WriteLine(a.Doctors);
- Console.ReadLine();
- }
- }
As a student or as a Software Developer, you might have heard over and over again that Generics are type-safe and also C# is a type-safe language.
You might have also learned that Object Class is the Base Class for all .Net Classes. So, what I’m trying to focus on here is, for an instance if you think,
Why do we need generics? Don’t we have Standard Collections?
To answer, let us dig more into Generics and Collections. The Primary idea of using Generics is, our “Standard Collections” can’t do an effective type-checking. If it cannot do an effective type checking, obviously it is not our friend, because as I pointed out earlier we are dealing with type-safe because our C# is a type-safe language and this guy here “Standard Collection” is not good at type-safe. So we ended up with Generics, a beautiful and powerful feature introduced in C# 2.0. Choosing Generics we're aiming at significant performance boost and quality code and without duplicating our code we're reusing our data. So, we are achieving reusability here.
Generics are also helpful in other areas of .Net such as arrays, reflection, serialization, collections, remoting, etc.
You can dig more into Generics
here.
Hope this helps, and Happy Coding!