Microsoft Introduces Direct C# File Execution in .NET 10 Preview 4
Microsoft has unveiled an exciting new capability in .NET 10 Preview 4 that fundamentally changes how developers can work with C#. The new dotnet run app.cs command allows developers to execute C# files directly, eliminating the traditional requirement for project files and complex scaffolding.
Breaking Down Barriers to Entry
Historically, running C# code required creating a complete project structure with .csproj files, which could be intimidating for beginners or cumbersome for quick experiments. This new feature, dubbed “file-based apps,” transforms C# into a more approachable language that competes directly with scripting languages like Python and JavaScript.
Powerful New Directives
The feature introduces several file-level directives that bring project-like capabilities to standalone files:
#:package– Add NuGet package references directly in your .cs file#:sdk– Specify different SDKs (perfect for web applications using ASP.NET Core)#:property– Configure MSBuild properties for advanced language features- Shebang support – Create executable C# shell scripts on Unix-like systems
Seamless Growth Path
Perhaps most importantly, this isn’t a separate runtime or dialect. When your simple script grows into a complex application, the dotnet project convert app.cs command automatically transforms it into a full project structure, preserving all your code and translating directives into proper MSBuild configurations.
Community Reception
The developer community has responded enthusiastically, particularly praising its potential for education and rapid prototyping. While some developers have requested additional features like REPL functionality and double-click execution, Microsoft has indicated that security considerations will guide future enhancements.
Getting Started
To try this feature, developers need to install .NET 10 Preview 4 and optionally use Visual Studio Code with the C# Dev Kit extension. The feature works immediately with any .cs file, making it as simple as creating a file and running dotnet run filename.cs.
This development represents Microsoft’s commitment to making C# more accessible while preserving the power and sophistication that makes .NET a enterprise-grade platform.
Source: https://devblogs.microsoft.com/dotnet/announcing-dotnet-run-app/