Generic Classes
Context: Generic classes allow you to define a class template with type parameters. This avoids duplicating code for different data types. The actual type is specified at instantiation. Generic classes provide compile-time type safety and improve performance by avoiding boxing/unboxing for value types.
Usage Example
Section titled “Usage Example”using System;
public class Box<T>{ private T _content;
public void Add(T content) { _content = content; }
public T Get() { return _content; }}
class Program{ static void Main() { Box<int> intBox = new Box<int>(); intBox.Add(42); int value = intBox.Get(); Console.WriteLine(value);
Box<string> stringBox = new Box<string>(); stringBox.Add("Hello"); Console.WriteLine(stringBox.Get()); }}Output console
Section titled “Output console”dotnet run42HelloImportant notes
Section titled “Important notes”- Type parameters are placed between angle brackets
<>after the class name. - Value types are not boxed when used with generics.
- A generic class can have multiple type parameters.
Real-world usage example
Section titled “Real-world usage example”.NET Collections – List<T>, Dictionary<TKey, TValue> and Queue<T> are generic classes used daily to store typed data.
See .NET docs on generic classes.