Byte Blogger

- Charan Pasham

111 Views

Executing single file csharp files - No .csproj Required

Did you know you can execute a standalone .cs file without creating a project or solution?

With .NET 10 (C# 14), you can run C# files directly using dotnet run.

 Console.WriteLine("Hello world");
 #!dotnet run
 Console.WriteLine("Hello world");
       chmod +x hello.cs

Reading command line arguments.

    if (args.length > 0)
    {
      string message = string.Join(' ', args);
      Console.WriteLine(message)
    }
    ./hello -- This is a command line.

The -- option indicates the all following arguments to be passed to the program.

Import nuget packages

#!dotnet run
#:package TextCopy@6.1.0

using TextCopy;

Console.WriteLine(await ClipboardService.GetTextAsync());

For more info

File-Based-Programs