Skip to content

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.

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());
}
}
Terminal window
dotnet run
42
Hello
  • 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.

.NET CollectionsList<T>, Dictionary<TKey, TValue> and Queue<T> are generic classes used daily to store typed data.
See .NET docs on generic classes.