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
dotnetCLI and your.csfile.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
#:packagedirective.#: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.WebMSBuild Properties: Set build properties like language version using the
#:propertydirective.#:property LangVersion previewShebang 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.cswith 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.




