Generic Classes
Reusable container types.
Generic Classes is a free C# Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Generic Class?
A generic class declares one or more type parameters in angle brackets after the class name. Those parameters can be used for fields, properties, method signatures, and return types throughout the class.
This lets you write one container or service that works for many element types while staying strongly typed.
class Box<T>
{
public T Value { get; set; }
}Constructing a Generic Class
To create an instance you supply a concrete type argument. Box<int> and Box<string> are distinct constructed types.
Each constructed type behaves as if its T were replaced by the real type everywhere.
using System;
class Box<T> { public T Value { get; set; } }
class Program
{
static void Main()
{
var b = new Box<int> { Value = 7 };
Console.WriteLine(b.Value);
}
}Generic Fields and Properties
Type parameters work as the type of fields and properties. A stack, queue, or cache often stores its data in an array or list of T.
The internal storage stays type-safe with no casting.
class Pair<T>
{
public T First;
public T Second;
}Multiple Type Parameters
A class can take several type parameters. A key/value pair or dictionary-style type commonly uses two.
Descriptive names such as TKey and TValue improve readability when more than one is present.
class Entry<TKey, TValue>
{
public TKey Key { get; set; }
public TValue Value { get; set; }
}A Simple Generic Stack
Here is a small generic stack built on a List<T>. Push adds to the end and Pop removes from the end.
The same class serves ints, strings, or any reference or value type.
using System;
using System.Collections.Generic;
class MyStack<T>
{
private readonly List<T> items = new();
public void Push(T item) => items.Add(item);
public T Pop()
{
var i = items[^1];
items.RemoveAt(items.Count - 1);
return i;
}
}
class Program
{
static void Main()
{
var s = new MyStack<string>();
s.Push("a");
s.Push("b");
Console.WriteLine(s.Pop());
}
}Generic Methods Inside Generic Classes
A generic class can also declare additional type parameters on individual methods. Those method-level parameters are independent of the class-level ones.
This adds flexibility, for example converting the stored type to another type.
class Box<T>
{
public T Value;
public TOut Map<TOut>(Func<T, TOut> f) => f(Value);
}Constraints on the Class
Like methods, a generic class can constrain its type parameters with where. A constraint applies to every member that uses that parameter.
Below, where T : class allows null assignment and reference comparisons.
class Repository<T> where T : class
{
private T current;
public bool IsEmpty => current is null;
}Static Members Are Per-Type
Each constructed generic type gets its own copy of any static fields. Counter<int> and Counter<string> track separate counts.
This is a subtle but important difference from non-generic classes.
class Counter<T>
{
public static int Count;
}Default Values in Generic Classes
Inside a generic class you often need a starting value for a field of type T. Use default(T) or the shorter default when the type is known from context.
It yields null for reference types and the zero value for value types.
class Slot<T>
{
public T Value = default!;
public bool HasValue { get; private set; }
}Inheriting From a Generic Class
You can derive from a generic base. The subclass may fix the type argument or remain generic itself.
Fixing it specializes the base; staying generic forwards the parameter upward.
class Box<T> { public T Value; }
class IntBox : Box<int> { }
class NamedBox<T> : Box<T> { public string Name; }Generics and the Built-In Collections
The .NET base library is full of generic classes: List<T>, Dictionary<TKey, TValue>, and Queue<T>. They are the everyday payoff of generic class design.
Writing your own generic types lets you extend that same safe, reusable style.
using System.Collections.Generic;
var scores = new Dictionary<string, int>();
scores["ada"] = 95;Quick Check
Test your understanding of generic classes.
Recap
Generic classes declare type parameters after the class name and use them for fields, properties, and methods. You construct them with concrete type arguments, creating distinct types.
They can add method-level type parameters, carry where constraints, be inherited, and hold per-type static state. Built-in collections are the prime example.
Frequently asked questions
Is the “Generic Classes” lesson free?
Yes — the full text of “Generic Classes” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Generic Classes”?
Reusable container types. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Generic Classes” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Generic Methods
- Generic Classes
- where Constraints
- Generic Interfaces