Ziggy Rafiq
CSharp 13 and .NET 9 Scripting Guide (Run .cs Without Projects)
Skip the hassle of creating projects run C# 13 scripts instantly with just one file.
This eBook is your practical guide to using dotnet run app.cs for scripting without project files, making C# development faster, lighter, and more flexible than ever before.
dotnet run app.cs
From writing quick prototypes to running production-ready scripts, you’ll learn how to unlock the power of C# 13 scripting with hands-on examples, best practices, and real-world developer tips.
Inside, you’ll discover how to:
dotnet run
C# development evolved from traditional, project-heavy workflows to a modern, lightweight scripting approach enabled by .NET 8 and C# 13. The article describes how developers are now able to write and execute pure C# code in standalone files without scaffolding projects due to recent language and platform improvements, including top-level statements and global usings. In this chapter, we discuss the benefits of this new scripting model, including faster development cycles, reduced overhead, and seamless access to powerful .NET features.C# development evolved from traditional, project-heavy workflows to a modern, lightweight scripting approach enabled by .NET 8 and C# 13. The article describes how developers are now able to write and execute pure C# code in standalone files without scaffolding projects due to recent language and platform improvements, including top-level statements and global usings. In this chapter, we discuss the benefits of this new scripting model, including faster development cycles, reduced overhead, and seamless access to powerful .NET features.
C# 13 scripting can be fully leveraged with dotnet run if your development environment is properly configured, though the process is straightforward thanks to the modern .NET SDK's simplicity. A .NET 9 SDK (or newer) is required, which includes features for C# 13 and single-file scripting - you can download it from the official .NET website for Windows, macOS, or Linux. The dotnet --version command can be used to verify the setup, and a test project build can be used to confirm the language version. You can enhance your coding experience with rich IntelliSense, debugging, and advanced refactoring with tools like Visual Studio 2022 (Preview), Visual Studio Code with the C# Dev Kit, or JetBrains Rider. Installing the dotnet-script global tool is highly recommended if you want even more powerful scripting capabilities, such as NuGet package references and the ability to use .csx files. By following these steps, you'll be able to run modern C# 13 scripts without needing traditional scaffolding.
With the evolution of the .NET SDK, particularly in .NET 8 and C# 13, C# has moved beyond its traditional dependency on full project structures like .csproj and .sln files, allowing for a more streamlined scripting experience. Developers can now run standalone .cs source files directly using dotnet run app.cs, without scaffolding. The SDK automatically handles the entire compilation process behind the scenes — from finding the source file to generating a temporary in-memory project, applying default settings such as .NET 9.0 target framework, C# 13 language version, nullable context, implicit usings, and top-level statements. Using this tool, you can move from idea to execution in seconds, simplifying rapid prototyping, educational use cases, and simple utilities, while leveraging advanced C# 13 features like file-local types, primary constructors, enhanced pattern matching, list patterns, and concise lambda expressions. The improvements to C# 13 scripting make it a lightweight, yet powerful alternative to traditional scripting languages, while retaining the performance, type safety, and scalability benefits of the whole .NET framework.
With the power of source file compilation and top-level statements, running your first C# 13 script is remarkably simple once your environment is set up. The app.cs script file can be created in any text editor — including Visual Studio Code, Visual Studio IDE, JetBrains Rider, or Notepad — and your code can be written directly without the need for class names, namespaces, or Main() methods. It is enough to write Console simply.WriteLine("Hello from Ziggy Rafiq, this is a C# 13 script!"); The .NET SDK parses the file, creates a temporary in-memory project, compiles the code, and runs the program automatically when you execute' dotnet run app. cs'. If the .NET SDK is installed, your script will run identically across Windows, Linux, and macOS. By eliminating boilerplate, scaffolding, and configuration, you can focus purely on logic, making it ideal for learning, rapid experimentation, file processing, API calls, and small automation tasks that can be executed instantly.
Developers have two practical solutions depending on their needs for managing NuGet packages since inline package references aren't directly supported within .cs files. C# 13 scripting with dotnet run makes rapid prototyping easy. The first option is to create a minimal console project using dotnet new console, which generates a complete project structure where NuGet packages can be added via dotnet add package, offering full IDE support, IntelliSense, type safety, and long-term maintainability—perfect when scripts grow into larger utilities. With the dotnet-script tool, you can also write inline NuGet package references using #r "nuget:..." directives, which support .csx files and NuGet package references. One-time installation (dotnet tool install -g dotnet-script) allows developers to create pure, portable C# scripts with dynamic package management, making it ideal for demos, automation, and lightweight utilities. For scenarios requiring external libraries, dotnet-script provides a more comprehensive scripting experience, making both tools complementary, depending on the complexity of the project.
C# 13 introduces a powerful set of modern language features that extend into scripting, enabling developers to write expressive, concise, and maintainable code directly in standalone .cs files without scaffolding. In .NET 9, scripts can leverage primary constructors for cleaner object initialisation, advanced pattern matching (including list patterns) for more readable conditional logic, and file-local types for safer type encapsulation. Top-level statements eliminate boilerplate completely, letting you write executable code without declaring a Main method, while features such as interpolated string handlers, required members, lambda improvements, and enhanced nameof() expressions make scripts more readable and production-ready. By combining Roslyn with the SDK, dotnet run app.cs compiles and executes these features as if they were part of a traditional C# application, but with scripting flexibility. Whether automating tasks, building small tools, or exploring language capabilities, C# 13 scripting empowers developers with all the power of the language, completely cross-platform, project-free, and highly efficient.
C# 13 scripting offers tremendous flexibility and power, but following best practices ensures that your scripts remain clean, maintainable, and production ready as they grow in complexity. In addition to avoiding bloated codebases and simplifying maintenance, file-local types prevent type name collisions across files by keeping scripts small and modular by organising code into multiple files. For more advanced scripting scenarios, tools like dotnet-script allow inline NuGet package references for richer, fully self-contained scripts, while dotnet run remains ideal for rapid prototyping and experimentation without project files. During automation, robust error handling with try-catch blocks prevents silent failures. Locking the SDK version using global.json ensures consistency across development teams and continuous integration pipelines. Using modern C# 13 features to maximise code clarity, expressiveness, and scalability of multi-file scripts, such as primary constructors, advanced pattern matching, and top-level statements, improves scalability. By adopting these practices, developers can write robust, safe, and highly maintainable scripts that fully utilise C# 13's modern capabilities, without sacrificing simplicity or efficiency.
To avoid pitfalls and ensure stable scripts, developers should be aware of the limitations of C# 13 scripting with dotnet run app.cs. NuGet packages cannot be referenced directly within standalone source files, one of its most significant limitations. For inline package references, one can use dotnet-script with #r "nuget:..." or switch to full project files when external dependencies are required. The IDE also lacks IntelliSense, debugging, error squiggles, and code navigation for single-file scripts in many popular editors. In addition to adding extensions, lightweight editors like VS Code can be used to create full projects. Also, single-file scripts lack native support for configuration files like appsettings.json, but this can be mitigated by using environment variables or passing runtime arguments directly, providing flexibility and security. With the right understanding and workarounds, developers can build, maintain, and scale robust C# 13 scripts while enjoying its modern features and the agility of pure source-file scripting.
Based on your specific needs, choosing the right C# 13 scripting development mode is crucial for balancing speed, flexibility, and scalability. It is fastest and most lightweight to run a standalone script using dotnet run app.cs for quick prototypes, simple experiments, or one-off tests, allowing instant execution without scaffolding the project. In case of external dependencies or richer scripting capabilities, dotnet-script becomes the best solution, enabling inline NuGet package references and maintaining a pure scripting experience. To ensure better structure, scalability, and professional-grade development workflows, it's best to use dotnet new console for production-ready scripts that involve multiple packages, robust configuration, full IDE features, and long-term maintainability.
C# 13 scripting has key limitations, and this chapter provides practical guidance on when to use lightweight scripts and complete .NET projects. Developers can choose the right approach for their needs by understanding these boundaries and following best practices—using scripting to perform quick tasks, while opting for robust project structures for complex applications to ensure maintainability and scalability.
To build maintainable and scalable scripts for professional use, it is essential to create a production-ready C# 13 script template. Using an external appsettings.json file, this template integrates best practices like multi-file organization, robust error handling, and modern C# syntax with flexible configuration management. It is easy to extend the structure by separating concerns, such as configuration loading (Config.cs), service logic (JsonService.cs), and program flow (Program.cs). Through try-catch error handling, this approach enhances code readability, improves robustness, and facilitates easy adaptation to real-world applications, such as APIs, command-line tools, and automation scripts. The dotnet run command compiles and executes the code smoothly, while the clear configuration pattern ensures flexibility for diverse deployment scenarios.
It is convenient and fast to prototype and automate small tasks without the need for a full project when using dotnet run app.cs to execute C# 13 source files directly. Scripting across Windows, macOS, and Linux is seamless with this method, which supports command-line arguments, multi-file compilation, and all modern C# 13 language features. NuGet packages cannot be referenced inline, causing compiler errors when external libraries are used. To work around this, you can switch to dotnet-script for lightweight scripting with package references or create a full .NET project for more complex dependencies and IDE support. For quick, simple scripts, dotnet run app.cs is ideal, while for heavier or production scenarios, more structured approaches are preferable.
Using modern features like record types and built-in JSON serialization, this final advanced example illustrates how to write a clean and concise C# 13 script. By defining a simple Config record to represent a scheduled task and serializing it and printing it as formatted JSON, it demonstrates how powerful .NET libraries can be used to automate tasks quickly. Providing a comprehensive guide to effective, modern C# scripting across platforms, this chapter provides a cheat sheet for running C# script files, using NuGet packages with dotnet-script, checking SDK versions, and creating console projects.
As a result of C# 13 and .NET 8, scripting has become easier and more efficient thanks to streamlined commands and workflows. It covers running single or multiple C# source files quickly with dotnet run, managing NuGet package dependencies with dotnet-script and .csx files, checking your SDK version for full language support, and creating a complete .NET console project for more complex applications. Using modern language features and minimal setup, you can prototype, automate, and build cross-platform scripts quickly and easily.
A C# script can be turned into a globally runnable command-line tool, much like a native shell script or utility, in this chapter. You can run your C# scripts anywhere and with arguments just like native commands if you make your .cs file executable, add a simple shell script wrapper that calls dotnet run, and optionally place the script in a directory included in your system's PATH. Although ideal for quick utilities and personal automation, building and publishing a self-contained .NET console app is recommended for production-ready global tools.
Whether you’re a .NET developer, automation enthusiast, or hobby coder, this book will help you master C# 13 scripting and speed up your workflow.
Run, test, and deploy C# scripts effortlessly—no project files needed.
Unlock unlimited ebook downloads. Share it on your social profile.