bhylu.com

Advertisements

C# Arrays

Store a fixed set of session capacities and update an element only at a valid position.

How it works

An array owns a fixed number of equal-type element slots. Its Length is established when the array is created and does not change when an element is assigned. Array access is checked at runtime, but an exception is not a substitute for validating a user-selected position. New array elements receive their type’s default value, which may not be a meaningful complete domain object. Keep the difference between array length, a count stored in an element and the identity of a workshop session clear.

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;

internal static class Program
{
    private static void Main(string[] args)
    {
        int[] places = { 6, 8, 4 };
        int position = 1;
        if (position >= 0 && position < places.Length) places[position] = 9;
        Console.WriteLine($"Sessions: {places.Length}; middle: {places[1]}");
        Console.WriteLine($"New array default: {(new int[2])[0]}");
    }
}

Expected result

Sessions: 3; middle: 9
New array default: 0

Only the middle capacity changes; the number of sessions remains three. C# arrays are reference types, so assigning the array variable to another variable shares the array rather than copying its elements. Use a deliberate copy when independent edits are required. For a changing number of sessions, a List is usually more suitable.

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

Advertisements