bhylu.com

Advertisements

C# Schedule Project

Validate a day’s time windows and allow exact adjacency while rejecting overlapping sessions.

Your brief

A session window is a half-open interval measured in minutes from the start of one chosen day. Start is included and end is excluded, so a session ending at ten can be followed by another starting at ten. Validate bounds and positive duration, sort a copy by start and compare each interval with the previous end. This checks a proposed single-room schedule; it does not choose an optimal schedule or resolve timezones, dates, multiple rooms and daylight-saving transitions.

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.

Worked solution

Show the worked solution
C# 14 · Program.cs

using System;
using System.Linq;

internal static class Program
{
    private static void Main(string[] args)
    {
        Slot[] proposed = { new(120, 180), new(60, 120) };
        Console.WriteLine($"Adjacent accepted: {ValidSchedule(proposed)}");
        Console.WriteLine($"Overlap accepted: {ValidSchedule(new[] { new Slot(60, 130), new Slot(120, 180) })}");
    }

    internal static bool ValidSchedule(Slot[] slots)
    {
        int previousEnd = 0;
        foreach (Slot slot in slots.OrderBy(value => value.Start))
        {
            if (slot.Start < 0 || slot.End > 1440 || slot.Start >= slot.End || slot.Start < previousEnd) return false;
            previousEnd = slot.End;
        }
        return true;
    }
}

internal readonly record struct Slot(int Start, int End);

Expected result

Adjacent accepted: True
Overlap accepted: False

Sorting makes input order irrelevant without mutating the original array. A zero-length or out-of-day interval is rejected before it can update previousEnd. An empty proposed schedule is valid under this policy. Equal starts for positive-duration sessions conflict. If room identity is added, group and validate per room rather than treating all sessions as one resource.

Extend and test

Test empty input, exact day endpoints, negative starts, end 1441, zero duration, reversed windows, duplicate windows and a nested interval.

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

Advertisements