Generics in C# - Generic Classes
Introduction to Generic Classes
Generics in C# provide a way to create classes, methods, delegates, and interfaces that can operate with any data type. This enables you to define classes and methods with the placeholder for the data type, which can be specified when the class or method is instantiated or called. Generic classes are particularly useful for creating collection classes.
Defining a Generic Class
A generic class is defined with a type parameter. This parameter is specified in angle brackets <> after the class name. Here is a basic example:
public class GenericClass<T> { private T data; public GenericClass(T value) { data = value; } public T GetData() { return data; } }
In this example, T
is a type parameter that will be specified when an object of GenericClass
is created. This allows the class to store and return data of any type.
Instantiating a Generic Class
Once a generic class is defined, you can instantiate it with different data types. Here is how you can do it:
public class Program { public static void Main() { GenericClass<int> intInstance = new GenericClass<int>(10); Console.WriteLine(intInstance.GetData()); GenericClass<string> stringInstance = new GenericClass<string>("Hello Generics"); Console.WriteLine(stringInstance.GetData()); } }
The output will be:
Hello Generics
Benefits of Using Generic Classes
Using generic classes provides several benefits:
- Type Safety: Generic classes ensure that you get compile-time type checking, which reduces the risk of runtime errors.
- Code Reusability: You can create a single class definition that can handle various data types without code duplication.
- Performance: Generics can improve performance by eliminating the need for boxing and unboxing when working with value types.
Constraints on Generic Types
Sometimes you may want to restrict the types that can be used as type parameters in a generic class. This is done using constraints. Here is an example:
public class GenericClass<T> where T : class { private T data; public GenericClass(T value) { data = value; } public T GetData() { return data; } }
In this example, the where T : class
constraint ensures that the type parameter T
must be a reference type.
Multiple Type Parameters
A generic class can have multiple type parameters. Here is an example:
public class Pair<T, U> { private T first; private U second; public Pair(T first, U second) { this.first = first; this.second = second; } public T GetFirst() { return first; } public U GetSecond() { return second; } }
You can instantiate this class with different types for each parameter:
Pair<int, string> pair = new Pair<int, string>(1, "One"); Console.WriteLine(pair.GetFirst()); // Output: 1 Console.WriteLine(pair.GetSecond()); // Output: One
Conclusion
Generic classes in C# are powerful tools that provide type safety, code reusability, and performance benefits. By understanding and utilizing generics, you can write more flexible and maintainable code. With constraints and multiple type parameters, generics offer extensive functionality to meet various programming needs.