C# Scripting (.Net 10)
Based on a YouTube video by Tim Corey.
Introduction
File based execution introduced in .Net 10 allows C# to be used like a scripting language, a simple file can be used as a complete application. This allows you execute a C# file, use packages, compile, etc.
Simple Demo #1
Create a folder for the quick demop and add a CSharp file, for example FileDemo.cs, and add the following code:
using System;
Console.WriteLine("Hello, World!");
This is just one file - no project or solution files, literally just this file. That is enough, just like a python file, batch file, etc. We can actually run this from the command line:
dotnet run FileDemo.cs
This should result in the following output:

Note: I had to run through a bunch of processed to make sure the C# 10 SDK was used to compile this.
This is quick and simple to use.
Command Line Arguments
It's also simple to add command line arguments to the script:
Console.WriteLine($"Hello, {args[0]}!");
Then run with the following (as an example):
dotnet run FileDemo.cs Everybody
This is a full C# application behind the scenes, so the output is as expected:

Note: We don't need to write System.Console.WriteLine($"Hello, {args[0]}!"); as System is one of C#'s implicit using statements.
User Input
Obviously we can accept user input, for example:
Console.Write("What is your name? ");
string? name = Console.ReadLine();
Console.WriteLine($"Hello, {name}!");
Adding a NuGet Package
Suppose we want to use a NuGet package, for example to use Spectre Console's ability to write to the console in red?
Simple, we can add a package (and version) with the following lines (as an example):
#:package Spectre.Console@0.54.0
using Spectre.Console;
So our updated could would look something like this as an example:
#:package Spectre.Console@0.54.0
using Spectre.Console;
AnsiConsole.MarkupLine($"[blue]What is your name?[/] ");
string? name = Console.ReadLine();
AnsiConsole.MarkupLine($"[green]Hello[/], [red]{name}[/]");
With the output being:

Make the Output an EXE
Because .Net Core is cross platform, we can run this on any machine with .Net runtime installed (for example Linux or MacOS). However, we can turn this into an executable (either a .dll or .exe) with the following command:
dotnet publish FileDemo.cs
Note: I actually had to run the following command to get the compiler to work as I don't appear to have the C++ build tools installed:
dotnet publish FileDemo.cs --framework net10.0 --runtime win-x64 --self-contained false -p:PublishAot=false
The key points:
--framework net10.0specifies .NET 10--runtime win-x64targets Windows x64--self-containedfalse creates a framework-dependent deployment (smaller size)-p:PublishAot=falsedisables Native AOT compilation (which requires C++ build tools)
To get around this and use the original dotnet publish FileDemo.cs command I needed to install the C++ Build tools via the Visual Studio Installer workload.
This will generate an executable for Windows.

Turning This Into a Project
Suppose this was just a PoC and now we want to extend this (add additional source files, etc.) and promote it to a full project. We do this by issuing the following command:
dotnet project convert FileDemo.cs
This will prompt for an output directory, just pressing [Enter] will save to the default location, in this case a folder called FileDemo. The source file is automatically re-written:
using Spectre.Console;
AnsiConsole.MarkupLine($"[blue]What is your name?[/] ");
string? name = Console.ReadLine();
AnsiConsole.MarkupLine($"[green]Hello[/], [red]{name}[/]");
and a project file is added:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<PackAsTool>true</PackAsTool>
<UserSecretsId>FileDemo-fadd3ecc94cfa778b37fb38eac5990f94ae99c1c22c09273a16003cf0bb55823</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Spectre.Console" Version="0.54.0" />
</ItemGroup>
</Project>
You'll see the project file has a package reference to any NuGet packages, in this case Spectre.Console.
Basically, the csproj is created based upon what was in our original file. It takes care of expanding the source file into a full project.
Things to consider
The generated executable files are created with native AOT (ahead of time compilation - so executes as fast as possible). To turn this off (in the original source) we can add the following alongside any package definitions:
#:property PublishAot=false
You'll see an equivalent line in the generated csproj file above.
Summary
The scripting functionality shown here is great for getting a solution out quickly without the usual overhead of creating projects, etc. It works well with simple proof of concepts or for a quick demo, and as shown can later be converted into a full project if required.
This approach can often be used to replace traditional console applications.