Introducing File-Based C# Apps


Traditionally, executing C# code required setting up a project structure with a .csproj
file. With this new feature, developers can run standalone .cs
files directly, akin to scripting languages like Python or JavaScript. This approach lowers the entry barrier for newcomers and accelerates the development workflow for seasoned programmers.
Key Advantages:
Immediate Execution: Run C# code instantly without project scaffolding.
Simplified Tooling: No additional tools or dependencies required—just the
dotnet
CLI and your.cs
file.Scalable Development: Easily transition from a single script to a full-fledged project as your application grows.
Enhancing Scripts with File-Level Directives
.NET 10 introduces file-level directives that bring additional capabilities to single-file C# applications:
NuGet Package References: Include external packages directly within your script using the
#:package
directive.#:package Humanizer@2.14.1 using Humanizer; var releaseDate = DateTimeOffset.Parse("2024-12-03"); var duration = DateTimeOffset.Now - releaseDate; Console.WriteLine($"Released {duration.Humanize()} ago.");
SDK Specification: Define the SDK context (e.g., for web applications) with the
#:sdk
#:sdk Microsoft.NET.Sdk.Web
MSBuild Properties: Set build properties like language version using the
#:property
directive.#:property LangVersion preview
Shebang Support: Create executable scripts on Unix-like systems with shebang lines.
#!/usr/bin/dotnet run Console.WriteLine("Hello from a C# script!");
Make the script executable and run it:
chmod +x app.cs
./app.cs
Seamless Transition to Project-Based Applications
When your script evolves into a more complex application, you can convert it into a standard project effortlessly:
dotnet project convert app.cs
Getting Started
Install .NET 10 Preview 4: Download and install from the official .NET website.
Set Up Your Editor: For Visual Studio Code, install the C# Dev Kit and switch to the pre-release version of the C# extension to enable file-based app support.
Write Your Script: Create a file named
hello.cs
with the following content:Console.WriteLine("Hello, world!");
Run Your Script: Execute the script using the command:
dotnet run hello.cs
Embracing a More Accessible C
The introduction of dotnet run app.cs
marks a significant step towards making C# more approachable and versatile. By streamlining the execution of C# code, Microsoft empowers developers to experiment, learn, and build applications more easily and efficiently.
Referrences
Subscribe to my newsletter
Read articles from Renato Ramos Nascimento directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Renato Ramos Nascimento
Renato Ramos Nascimento
With over 14 years in software development, I specialize in backend systems using .NET, Python, and Java. I bring full lifecycle expertise, including requirements analysis, client/server and data layer development, automated testing (unit, integration, end-to-end), and CI/CD implementations using Docker, GitLab Pipelines, GitHub Actions, Terraform, and AWS CodeStar.