New capability in .NET 10 Preview 4 allows developers to run a stand-alone C# file directly, without requiring a project file or project structure.
Taking a lesson from scripting languages such as Python and JavaScript, Microsoft has introduced a file-based apps capability for the C# language, which is intended to streamline C# development.
Introduced in .NET 10 Preview 4, the new feature allows developers to run a stand-alone C# (.cs) file directly with the command, dotnet run app.cs
. Developers no longer need to create a project file or scaffold an entire application to test a test snippet, run a quick script, or experiment with an idea, wrote Damian Edwards, principal architect at Microsoft, in a May 28 blog post announcing the feature. Previously, running C# code using the dotnet CLI has required a project structure that included a .csproj file. Developers can get started with the feature by downloading .NET 10 Preview 4.
File-based apps lower the entry barrier to trying out C# and make the language a more-attractive choice for learning, prototyping, and automation scenarios, Edwards said. Developers get a quick start while no project file is required, there is “first-class” CLI integration, and the capability scales to real applications. There is no separate dialect or runtime; when a script grows up, it can evolve into a full-fledged project using the same language, syntax, and tools. With .NET 10 Preview 4, file-based apps also support a set of file-level directives to declare packages, SDKs, and properties (which are stored in project files for project apps) without leaving a .cs file.
Microsoft with dotnet run app.cs
believes it is making C# more approachable while preserving the power and depth of the .NET ecosystem. Upcoming .NET previews will aim to improve the experience of working with file-based apps in Visual Studio Code, with enhanced IntelliSense for new file-based directives, improved performance, and debugging support, Edwards said. For the command line, Microsoft is looking into support for file-based apps with multiple files and ways to make running file-based apps faster. Microsoft asks developers to try out the capability and send feedback to GitHub.