bhylu.com

Advertisements

C# Methods

Pass small values into a named calculation and make optional and named arguments easy to interpret.

How it works

A method declares its parameter and result types so calls can be checked. Pass small scalar inputs by value and return the natural result when possible. A default parameter supplies an omitted argument at the call site, not a mutable configuration setting. Named arguments help explain several same-typed inputs, but a method with too many parameters may need a meaningful request type. This small helper requires nonnegative minutes whose sum fits int; later checked domain methods enforce stronger contracts.

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)
    {
        Console.WriteLine($"Default: {PlannedMinutes(40, 10)}");
        Console.WriteLine($"Custom: {PlannedMinutes(teaching: 40, setup: 10, reserve: 8)}");
    }

    internal static int PlannedMinutes(int teaching, int setup, int reserve = 5)
    {
        return teaching + setup + reserve;
    }
}

Expected result

Default: 55
Custom: 58

The first call supplies the default five-minute reserve, while the named-argument call states each role. Parameters receive values and assigning a parameter would not reassign the caller variable. Instance methods additionally operate through an object reference; static methods have no implicit instance. Separate pure calculation from I/O when that makes testing clearer.

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

Advertisements