Skip to content

File-based app directives C# 14.0new scenarios

New preprocessor directives for configuring file-based apps.

.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 Newtonsoft.Json@13.0.3
#: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 #:package for NuGet references and #:property for MSBuild properties

More information