.NET 10 introduces a file-based app model where a single .cs file can be a complete application without a project file. C# 14 adds new #: preprocessor directives to configure these apps directly within the source file — specifying package references, project properties, and more.
Code
C#
#:package [email protected]
#:property LangVersion preview
using Newtonsoft.Json;
var obj = new { Name = "Hello", Value = 42 };
Console.WriteLine(JsonConvert.SerializeObject(obj));C#
// Required a .csproj file alongside your code:
//
// <Project Sdk="Microsoft.NET.Sdk">
// <PropertyGroup>
// <OutputType>Exe</OutputType>
// </PropertyGroup>
// <ItemGroup>
// <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
// </ItemGroup>
// </Project>
using Newtonsoft.Json;
var obj = new { Name = "Hello", Value = 42 };
Console.WriteLine(JsonConvert.SerializeObject(obj));Notes
- Directives use the
#:prefix to distinguish them from existing#preprocessor directives - Only valid in file-based apps (single-file programs run with
dotnet run file.cs) - Supports
#:packagefor NuGet references and#:propertyfor MSBuild properties