DEV Community

Cover image for .NET Run C# Single Files Without a Project — Almost Like Python
Arash zandi
Arash zandi

Posted on

.NET Run C# Single Files Without a Project — Almost Like Python

Introduction

The .NET platform is Microsoft’s cross-platform, open-source runtime and SDK for building applications that run from tiny IoT devices to large cloud services. Its primary language, C#, compiles to efficient native code and offers modern features for productivity and safety. Historically, developers began with a project (.csproj) and solution structure; the .NET CLI (dotnet) then built and ran that project. Starting with C# 14 / .NET 10, you can also run a single C# source file directly—an approach called a file-based app—and in .NET 10 that experience received major upgrades.
This article introduces the file-based model, explains the *.NET 10 * enhancements (notably native AOT publishing), and walks through step-by-step examples you can try today.

I'll show you step by step :

1 . First download .Net SDK SDK 10.0.100-preview.4 or newer from this link :
Download .NET 10.0 and install it

2 . Create a folder in your machine and then create a Program.cs file inside the folder. (you can now create and run any .cs file and the name not matters)

3 . Write this code in Program.cs

Console.WriteLine("Hello, World!");
Enter fullscreen mode Exit fullscreen mode

4 . then open the folder containing Program.cs file in the terminal , CMD or even VsCode Terminal and run this command

Dotnet run Program.cs

5 . Finish... This is your result :

Result of running

Now let's have more details :

using nuget packages

Suppose that you want to use a nuget package in your app like Newtonsoft.Json and use it in your codes

  • add this line in top of your .cs file :
#:package Newtonsoft.Json@13.0.3
Enter fullscreen mode Exit fullscreen mode

Or for using latest version

#:package Newtonsoft.Json@*
Enter fullscreen mode Exit fullscreen mode
  • Then write the using :
using Newtonsoft.Json;
Enter fullscreen mode Exit fullscreen mode
  • Now You can use the Newtonsoft.Json in your project and this is my sample :
#:package Newtonsoft.Json@13.0.3

using Newtonsoft.Json;

Console.WriteLine("Hello, World!");

var date = new
{
    date = DateTime.Now,
    message = "This is a test message"
};

string jsonString = JsonConvert.SerializeObject(date, Formatting.Indented);

Console.WriteLine(jsonString);
Enter fullscreen mode Exit fullscreen mode

And the result is something like this :

PS C:\Users\ArashZandi\Desktop\C# file> dotnet run program.cs
Hello, World!
{
  "date": "2025-08-22T15:16:44.4952004+03:30",
  "message": "This is a test message"
}
Enter fullscreen mode Exit fullscreen mode

Making an API with just a single file

1 . Make a file with your desired name (mine is Api.cs)
2 . Write this codes :

#:sdk Microsoft.Net.Sdk.Web

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/test", () => "Hello World!");
await app.RunAsync();
Enter fullscreen mode Exit fullscreen mode

3 . Now run the project with the command Dotnet run Api.cs
4 . The result is

PS C:\Users\ArashZandi\Desktop\C# file> dotnet run api.cs
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Users\ArashZandi\Desktop\C# file
Enter fullscreen mode Exit fullscreen mode

5 . You see that there is a website listening this address http://localhost:5000 Now in your browser go to this link
http://localhost:5000/test

6 . You should see the Hello World! comming from the API (read about Minimal Apis more in the references) as the result.

Tip : the key point of this type of .cs file is this line #:sdk Microsoft.Net.Sdk.Web which allow you to use WebApplication

Converting the .cs file to a .csproj based project

You can convert your .cs file to the other type which uses .csproj file like this :

dotnet project convert program.cs
dotnet project convert api.cs
Enter fullscreen mode Exit fullscreen mode

Now I've converted my projects one by one in their own folders.

Referencing to your other projects in the .cs file

My Program.cs and Api.cs were in a same folder, I've converted them and I have this file/folder tree

File/Folder tree

Now I want to use my Program.csproj in the Api.cs (single file)
Actually I want to have a reference of Program.csproj (the converted proj) into Api.cs (the single file project)

1 . I delete converted Program.cs project and modify my program.cs file like this :

#:package Newtonsoft.Json@13.0.3

using Newtonsoft.Json;

Console.WriteLine("Hello, World!");

var date = new
{
    date = DateTime.Now,
    message = "This is a test message"
};

string jsonString = JsonConvert.SerializeObject(date, Formatting.Indented);

Console.WriteLine(jsonString);

public class ProgramTestClass
{
    public static string test() => "This is a test method.";
}
Enter fullscreen mode Exit fullscreen mode

I convert it again to a .csproj project

dotnet project convert program.cs
Enter fullscreen mode Exit fullscreen mode

Now in my Api.cs file I can use this line
#:project ./program/program.csproj

And my codes in Api.cs are these :

#:sdk Microsoft.Net.Sdk.Web
#:project ./program/program.csproj

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

var stringFromReferencedProj = ProgramTestClass.test();
app.MapGet("/test", () => stringFromReferencedProj);
await app.RunAsync();
Enter fullscreen mode Exit fullscreen mode

The result of this link
http://localhost:5000/test
is :
This is a test method.

When to choose file-based apps (and when not)

File-based apps shine for small CLI tools, teaching, experiments, or build scripts—situations where you want to get from idea to running code with minimal ceremony. As complexity grows (multiple files, tests, multi-targeting, analyzers), migrate to a traditional project structure. The official tutorial and ecosystem write-ups emphasize this pragmatic split

Conclusion

.NET 10 turns file-based apps from a neat trick into a capable development mode. You can run C# files directly, pull in NuGet packages and other projects with declarative #: directives, publish native executables by default, and even host a minimal web API in a single file. The result is a lower-ceremony on-ramp to C# that still scales into project-based development when your program outgrows one file

References to read more :

Top comments (0)