Generics in C# are one of the most frequently asked interview topics.
What Are Generics in C#?
Generics allow you to define classes, methods, interfaces, and delegates with a placeholder type.
Instead of writing separate code for each type (int, string, etc.), you write code once and reuse it for multiple types.
Without Generics
public class Box
{
public object Value { get; set; }
}
Usage:
Box box = new Box();
box.Value = 10;
int value = (int)box.Value; // Type casting required
Problems (Interview Point)
-
❌ No type safety
-
❌ Runtime errors possible
-
❌ Boxing & Unboxing (performance issue)
With Generics
public class Box<T>
{
public T Value { get; set; }
}
Usage:
Box<int> box = new Box<int>();
box.Value = 10;
int value = box.Value; // No casting required
Benefits (Very Important for Interviews)
-
✅ Type Safety
-
✅ No Casting
-
✅ Better Performance
-
✅ Code Reusability
Generic Class Example
public class Calculator<T>
{
public T Add(T a, T b)
{
dynamic x = a;
dynamic y = b;
return x + y;
}
}
Usage:
Calculator<int> calc = new Calculator<int>();
Console.WriteLine(calc.Add(5, 10));
Calculator<double> calc2 = new Calculator<double>();
Console.WriteLine(calc2.Add(5.5, 2.2));
Generic Method Example
You can also use generics only at method level
public class Utility
{
public static void Print<T>(T value)
{
Console.WriteLine(value);
}
}
Usage:
Utility.Print<int>(10);
Utility.Print<string>("Hello");
Utility.Print<double>(10.5);
Generic List Example (Real World Example)
This is the most common usage:
List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(20);
Without Generics (Old Way)
ArrayList list = new ArrayList();
list.Add(10);
list.Add("Hello"); // Problem: mixed types
Generics solve this issue.
Generic Constraints (Very Important Interview Topic)
You can restrict generic types using constraints.
where T : class
public class MyClass<T> where T : class
{
}
Means T must be a reference type
where T : struct
public class MyStruct<T> where T : struct
{
}
Means T must be a value type
where T : new()
public class MyClass<T> where T : new()
{
public T Create()
{
return new T();
}
}
Means T must have parameterless constructor
Multiple Constraints
public class MyClass<T> where T : class, new()
{
}
Generic Interface Example
public interface IRepository<T>
{
void Add(T item);
List<T> GetAll();
}
Implementation:
public class Repository<T> : IRepository<T>
{
private List<T> items = new List<T>();
public void Add(T item)
{
items.Add(item);
}
public List<T> GetAll()
{
return items;
}
}
Generic Delegate Example
public delegate T MyDelegate<T>(T value);
Usage:
MyDelegate<int> del = x => x * 2;
Console.WriteLine(del(10));
Common Interview Questions
1. What are generics?
Generics allow defining classes, methods, interfaces with type parameters, improving type safety and performance.
2. Why generics are better than object type?
| Object | Generics |
|---|---|
| No type safety | Type safe |
| Casting required | No casting |
| Boxing/Unboxing | No boxing |
| Runtime errors | Compile-time safety |
3. What is T in Generics?
T is a type parameter placeholder
Example:
class MyClass<T>
T can be replaced with:
-
int
-
string
-
custom class
4. Can we use multiple generic types?
Yes:
public class MyClass<T, U>
{
public T First { get; set; }
public U Second { get; set; }
}
Usage:
MyClass<int, string> obj = new MyClass<int, string>();
Real World Example
Repository Pattern
public interface IRepository<T>
{
T GetById(int id);
void Add(T entity);
}
Now reuse for:
-
User
-
Product
-
Order
No need to write separate code.
Quick Interview Summary
Generics provide:
-
Type safety
-
Reusability
-
Performance
-
Clean code
Used in:
-
List
-
Dictionary<TKey, TValue>
-
Repository Pattern
-
LINQ
