# 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.
    
    ```csharp
    #: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`
    
    ```csharp
    #:sdk Microsoft.NET.Sdk.Web
    ```
    
* **MSBuild Properties**: Set build properties like language version using the `#:property` directive.
    
    ```csharp
    #:property LangVersion preview
    ```
    
* **Shebang Support**: Create executable scripts on Unix-like systems with shebang lines.
    
    ```csharp
    #!/usr/bin/dotnet run
    Console.WriteLine("Hello from a C# script!");
    ```
    

Make the script executable and run it:

```bash
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:

```bash
dotnet project convert app.cs
```

## Getting Started

1. **Install .NET 10 Preview 4**: Download and install from the official [.NET website](https://dotnet.microsoft.com).
    
2. **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.
    
3. **Write Your Script**: Create a file named `hello.cs` with the following content:
    
    ```csharp
    Console.WriteLine("Hello, world!");
    ```
    

**Run Your Script**: Execute the script using the command:

```bash
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

* [https://devblogs.microsoft.com/dotnet/announcing-dotnet-run-app/](https://devblogs.microsoft.com/dotnet/announcing-dotnet-run-app/)
