Sunday, June 22, 2025

Super Simple .NET Run Program.cs

From .NET 10 Preview 4 onwards, we can run individual .NET Code files without any ceremony just like you would do in Python etc.

For example, I can just create a .cs file (HelloWorld.cs) with some C# code.
Console.WriteLine("Hello World!");
And then do the following.
dotnet run HelloWorld.cs
dotnet run HelloWorld.cs
If we need to use external packages within our code, we can do something like this. Here we don't have a .csproj file, so we can do the package reference inline. For demo purposes, I am using  Humanizer package.
// Reference the Humanizer package
#:package Humanizer@2.*

// Using directive
using Humanizer;

// Using the package
Console.WriteLine("Hello World!".Humanize(LetterCasing.AllCaps));
dotnet run HelloWorld.cs
That's neat!

Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment