C# Composition
Keep a workshop’s identity, date and capacity together without hiding unrelated responsibilities in one large class.
How it works
Composition builds a type from smaller concepts it contains or depends on. A scheduled workshop has a title and a date; it is not a subtype of a calendar date. A collaborator can also be supplied as a constructor argument so the owner’s dependency is visible. Decide which type validates cross-field rules, such as a booking date and session window, rather than assuming each individually valid field makes the combination valid.
Run it locally
Use the .NET 10 SDK with C# 14 in a separate console lab. Create an empty project once with dotnet new console --framework net10.0, then replace Program.cs with this complete source and add any supporting files shown. Keep only one lesson entry point in the project. C# does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed.
dotnet build -p:LangVersion=14.0 -p:Nullable=enable -p:TreatWarningsAsErrors=true
dotnet run --no-buildUse the SDK’s project workflow; the review compiles against its matching reference assemblies and executes the results without external NuGet packages. File-writing examples belong in a private empty lab directory.
Example
using System;
using System.Globalization;
internal static class Program
{
private static void Main(string[] args)
{
var workshop = new ScheduledWorkshop("Paper repair", new DateOnly(2026, 11, 7), 8);
Console.WriteLine($"{workshop.Title}: {workshop.Capacity} places");
Console.WriteLine(workshop.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
}
}
internal sealed record ScheduledWorkshop(string Title, DateOnly Date, int Capacity);
Expected result
Paper repair: 8 places
2026-11-07
The example keeps a fixed valid fixture readable. A public constructor should enforce title and capacity rules or accept already-validated domain values. The record is an owned data description, not a service that sends messages, writes files and stores credentials. Separating those boundaries makes later tests smaller and makes it easier to see where failures can occur.
Consult Microsoft’s C# documentation for language and library contracts, including target-framework and version requirements.