'C# 9 top-level programs without csproj?

One feature of coming C# 9 is so called top-level programs. So that you could just write the following without classes.

using System;

Console.WriteLine("Hello World!");

and dotnet run will launch it for you.

It works for me, but only if I also add a .csproj file like the one below

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.0</TargetFramework>
    </PropertyGroup>
</Project>

Is there a way to skip .csproj from the picture? :) So that there's just a single Program.cs file and nothing more.



Solution 1:[1]

You can create a C# script file (.csx) and run it with the dotnet script tool from

https://github.com/filipw/dotnet-script

But not a true, valid, C# file no

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Erik Karlsson