Running C# without csproj files

05/26/2025
3 minute read

csproj is gone!

In .NET 10 there is no need to have a csproj file to get started with C# and run applications, even complex applications like creating a Web API!

First, you need to have .NET SDK installed on your machine, obviously 😅 At the time of writing this post it is 10.0.0-preview.4.

After installation, create a file, let's say app.cs and copy the following contents in it:

Console.WriteLine( "Hello, C#" );

Open a terminal at the location of the file and use

dotnet run App.cs

Importing NuGet packages

Isn't that amazing! Well, what if we need to add a dependency to it, in this case we could use #:package preprocessor to include a NuGet package that is needed, I am using the same example as what was shown at the build, so consider the App.cs file has the following content:

var buildDate = new DateTime(2025, 05, 22);

Console.WriteLine($"Build was {DateTime.Now - buildDate} ago!");

and by running the previous command the output would be something not really readable for humans:

❯ dotnet run App.cs
Build was 4.20:46:06.9317230 ago!

There is a package called Humanizer that we could use to make the output more readable, but to do so we need to import the package, this could be achieve by adding the following line to the beginning of the file:

#:package Humanizer@2.*

Then import the package and change the code to use Humanizer

#:package Humanizer@2.*

using Humanizer;

var buildDate = new DateTime(2025, 05, 22);
var since = DateTime.Now - buildDate;

Console.WriteLine($"Build was at {since.Humanize()} ago");

When running the application, it restores the packages by running dotnet restore behind the scene, and then runs the application!


Using SDKs

Now, let's make it a bit more complex, let's say we want to create a minimal API application and exposing an endpoint, for that purpose, we need to import the Web SDK.

Create a new file called, web-api.cs and use the #:sdk preprocessor:

#:sdk Microsoft.NET.Sdk.Web

#:package Humanizer@2.*

using Humanizer;

var builder = WebApplication.CreateBuilder();

var app = builder.Build();

app.MapGet("hello", () =>
{
  var buildDate = new DateTime(2025, 05, 22);
  var since = DateTime.Now - buildDate;
  return $"Build was {since.Humanize()} ago";
});

app.Run();

now run the application and you should have an output like the following, navigate to http://localhost:5000/hello and you should see a response back from the API


Back to the project files

This is an easy and concise way to start with prototyping a project or learning the language, however, a more robust solution requires proper separation between different projects. One might ask that now that we have started with the project-less approach, how could we convert this work into a project? well, it's easy, use the dotnet project convert command to convert this into a normal project. It will create a folder with the same name as the file, creates a csproj with proper package references and SDK references, and converts the file to a normal C# file:

dotnet project convert web-api.cs
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Humanizer" Version="2.*" />
  </ItemGroup>

</Project>

That's it so far; this is of course at the begging and Microsoft is trying to make engagement of new developer easier into the eco-system! Also, for the people already using C#, it would be easier to just kick start a new application.

PS: Multiple file support coming soon, stay tuned 😉


At the end, thanks for reading, enjoy coding and Dametoon Garm [**]

PS: The codes in this post are more or less borrowed from the following YouTube Video from Damian Edwards. He even shows how this could be combined with the magical #! and make a fully functional script file! Check it out for yourself:

Buy Me a Coffee at ko-fi.com

Test Your Roslyn Code Analyzers

In the previous post we have seen how it is possible to create a diagnostic analyzer using Roslyn APIs. Being a TDD advocate it would be disappointing not to talk about how we could test our code analyzer! So let's take a look into it.

Creating Code with Code - Introduction to Roslyn

Compilers used to be a black box, they were accepting a bunch of source files as input and were producing an artifact as an output! There were no means of interaction with any of the phases inside the compiler pipeline. However, with Roslyn, The .NET Compiler Platform SDK, it is not the case anymore!

Get started with ASP.NET Core for non-C# developers

I had a discussion on a meet-up a month ago with a friend of mine, and he was telling me it is not really easy or clear how one could create a simple web application in .NET and C#. Hopefully this post is going to help that friend of mine! 😊

An error has occurred. This application may no longer respond until reloaded. Reload x