Object-Oriented C#

Generics

Write reusable, type-safe classes and methods.

Introduction

Generics preserve concrete type information without duplicating implementation.

Box<T> can store any chosen type while preventing values of other types from being assigned accidentally.

Runnable C# example

C#
Run code →
Program.cs
using System;

class Box<T>(T value) { public T Value { get; } = value; }

class Program
{
    static void Main()
    {
        var box = new Box<int>(42);
        Console.WriteLine(box.Value);
    }
}
Result of running the code
42

Practice with the debugger

Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges