bhylu.com

Advertisements

C# List<T>

Add and remove bookings while separating Count from allocated Capacity.

How it works

List<T> owns a variable-length sequence of typed elements. Count is the number of elements that exist; Capacity is backing storage reserved for growth and is not permission to index additional elements. Adding and removing can shift positions, so an old index may identify a different booking after a change. Use stable identifiers for domain identity. A read-only interface can limit operations visible through one reference, but does not automatically make the underlying collection immutable.

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-build

Use 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.

Download the complete C# source as Program.cs.

Example

C# 14 · Program.cs

using System;
using System.Collections.Generic;

internal static class Program
{
    private static void Main(string[] args)
    {
        var counts = new List<int> { 3, 5 };
        counts.Capacity = 10;
        counts.Add(4);
        counts.RemoveAt(0);
        Console.WriteLine($"Bookings: {counts.Count}; first: {counts[0]}");
        Console.WriteLine($"Reserved enough: {counts.Capacity >= 10}");
    }
}

Expected result

Bookings: 2; first: 5
Reserved enough: True

The removal shifts five into position zero. The reserved capacity remains a storage detail, so the output checks only the documented lower bound. Removing matching items can use RemoveAll instead of editing the list structure during foreach. Copying a list copies element values, which still shares referenced objects when T is a reference type.

Consult Microsoft’s C# documentation for language and library contracts, including target-framework and version requirements.