bhylu.com

Advertisements

C# Command-line Arguments

Require one count argument and give a calling script separate normal output and error output.

How it works

The args array contains the user-supplied command-line strings for this entry-point form. Unlike C’s argc convention, it does not include the executable name as an ordinary first element. Check its length before indexing. Each argument still needs complete conversion and domain validation. Set a nonzero exit status when invocation is invalid so a caller does not have to parse a success-looking message to detect failure. Shell quoting is handled before the runtime supplies these strings.

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

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.Globalization;

internal static class Program
{
    private static void Main(string[] args)
    {
        if (args.Length != 1)
        {
            Console.Error.WriteLine("Usage: lesson COUNT"); Environment.ExitCode = 2; return;
        }
        if (!int.TryParse(args[0], NumberStyles.None, CultureInfo.InvariantCulture, out int count) || count is < 1 or > 20)
        {
            Console.Error.WriteLine("COUNT must be 1..20"); Environment.ExitCode = 2; return;
        }
        Console.WriteLine($"Requested: {count}");
    }
}

Expected result

Requested: 3

Invoke the program with argument three. With dotnet run, use -- before application arguments when necessary to separate them from tool options. The review invokes the compiled assembly with actual argument arrays and checks status, stdout and stderr separately. No shell command is constructed from the supplied count.

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