---
title: "Why Generics in C#"  
description: "In this blog I not tell you “What is generic class in C#?” you can read about generic from below link. Here I tell you “Why Generic in C# !”. First se"  
author: "AVADHESH PATEL"  
published: 2013-01-17  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/432/why-generics-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Why Generics in C#

In this blog I not tell you “What is generic [class in C#](https://www.mindstick.com/forum/160399/how-to-create-a-custom-generic-class-in-c-sharp-provide-an-example)?” you can read about generic from below link.\

[http://www.mindstick.com/Blog/165/Generic%20class%20in%20c](https://www.mindstick.com/Blog/165/Generic)

Here I tell you “Why Generic in C# !”. First see below code:

```
class mindstick{int nRegNo;string sAddress;}
```

**\**1. Whenever we define the class in C#, C++ or in Java, class can have several types of [variables](https://www.mindstick.com/articles/715/php-variables).

2. How in this case if we create an object, object consist several variables of different types.

3. Now we want to create [multiple object](https://www.mindstick.com/interview/34322/how-would-you-ensure-atomicity-across-multiple-object-stores-in-a-single-transaction) of same class but every object will have different type of [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) called collection.

4. Here come [boxing and unboxing](https://www.mindstick.com/articles/167412/boxing-and-unboxing).

Conclusion: need to take care about [conversion](https://www.mindstick.com/forum/1734/conversion-from-32-bit-integer-to-4-chars) before [assigning the data](https://www.mindstick.com/forum/34179/i-am-getting-error-while-assigning-the-data-to-the-array-list).

Now the main purpose of the using [generics](https://www.mindstick.com/articles/12420/generics-in-c-sharp-with-example), each object will have multiple variable of similar type. Now we can prevent boxing and unboxing.

If we apply generic on class then we can achieve static polymorphism.

```
class mindstick{int nRegNo;string sAddress;}
```

mindstick <int> ms1 = new mindstick<int>();

mindstick <string> ms2 = new mindstick<string>();

Now in above program, we have two objects belong to same class supporting different data type not in same.

This type of an object is called “Generic Object” and its class [known as](https://answers.mindstick.com/qa/35703/ricky-ponting-is-also-known-as-what) “Generic Class”. See below example.

```
class mindstick<A>{   public A assign(A a1)   {                return a1;   }}public class caller{   public static void Main()   {     mindstick <int> k1= new <int>();     mindstick <string> k2= new <string>();                 int t1 = int k1.assign(12); // No change to apply conversion     Console.WriteLine(t1);   }}
```

---

Original Source: https://www.mindstick.com/blog/432/why-generics-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
