Generics and Generic Classes in C#
In C#, Generics allow developers to design classes that can work with any data type without compromising type safety. A generic class is a class that defines one or more type parameters, which can be specified when creating an object of that class.
Why Use Generics
- Type Safety: Detect type errors at compile-time instead of runtime.
- Code Reusability: Write a single class or method that works with multiple data types.
- Performance: Avoids boxing/unboxing in value types, improving execution speed.
What is a Generic Class
A generic class is a class that takes a type parameter within angle brackets (< >) so that the class can operate on different data types while maintaining compile-time type safety.
It eliminates the need to write separate classes for each data type and reduces code duplication.
Syntax
class ClassName<T>{
// members using T
}
- T: Represents a placeholder type parameter.
- You can specify the type when creating an object.
Generic Class Example
using System;
class GenericClass<T>
{
private T data;
public GenericClass(T value)
{
data = value;
}
public void Display()
{
Console.WriteLine("Value: " + data);
}
}
class Program
{
static void Main()
{
GenericClass<int> obj1 = new GenericClass<int>(100);
obj1.Display();
GenericClass<string> obj2 = new GenericClass<string>("GeeksForGeeks");
obj2.Display();
}
}
Output
Value: 100 Value: GeeksForGeeks
Explanation:
- obj1 is created with int, so the generic type T is replaced by int.
- obj2 is created with string, so the generic type T is replaced by string.
Generic Class with Multiple Type Parameters
A generic class can also take two or more type parameters, which makes it possible to store or operate on values of different data types in a single class.
using System;
// Generic class with two type parameters
class Pair<T1, T2>
{
public T1 First { get; set; }
public T2 Second { get; set; }
public Pair(T1 first, T2 second)
{
First = first;
Second = second;
}
// Method to display values
public void Display()
{
Console.WriteLine($"First: {First}, Second: {Second}");
}
}
class Program
{
static void Main()
{
// Creating Pair with int and string
Pair<int, string> pair = new Pair<int, string>(1, "One");
pair.Display();
}
}
Output
First: 1, Second: One
Explanation:
- Pair<T1, T2>: A generic class is declared with two type parameters T1 and T2.
- The constructor takes one value of type T1 and another of type T2.
- In Main(), we create an object with int and string as type arguments, so T1 becomes int and T2 becomes string.
Key Points
- Generic classes improve type safety by avoiding explicit type casting.
- They are widely used in collection classes like List<T>, Dictionary<TKey, TValue>, and Queue<T>.
- Constraints in generics ensure that only suitable types are passed as parameters.
What is the main advantage of using Generics in C#?
-
A
They allow runtime type changes
-
B
They provide type safety, code reusability, and better performance
-
C
They remove the need for classes
-
D
They automatically convert between types
Generics detect type errors at compile-time, reduce duplication, and improve performance by avoiding boxing/unboxing.
What does the type parameter T represent in a generic class declaration like class ClassName<T>?
-
A
A fixed data type
-
B
A placeholder for any data type specified when creating an object
-
C
A keyword for type conversion
-
D
A runtime variable
What is the main purpose of using generics in C#?
-
A
To reduce memory usage only
-
B
To allow runtime type checking
-
C
To provide type safety and code reusability
-
D
To replace inheritance