The file app is a new feature in .NET 10 that lets you run a C# source file directly without requiring a project file, like dotnet App.cs. While the build system is tightly integrated with the project file, it operates virtually within the file-based app execution.
Project components like SDK, properties, references, and packages are now defined directly within the C# source file. This is done with the help of C# Ignored Directives. This ensures seamless integration with the virtual project file.
But it poses a minor issue. The project-based execution does not understand the C# source file that holds these directives. So, sharing source files becomes an issue.
Fortunately, the framework itself offers a workaround to solve this issue.
There is a framework-defined constant that distinguishes between file-based and project-based programs. It’s named FileBasedProgram. This is defined only within the context of file-based execution.
So, wrap the ignored directives using the C# preprocessor condition to make sure they are skipped when not relevant.
This approach lets you quickly test different parts of your app.
#if FileBasedProgram
#:package Humanizer.Core@3.*
#endif
using Humanizer;
var year2026 = new DateTime(2026, 1, 1);
// 27 days from now
Console.WriteLine(year2026.Humanize());
In VS2022, ignored directives will show a red squiggly. There’s no need to worry, as the program will work fine. VS2026 will handle them without any issues.
Happy coding. Stay connected as we continue to learn and share the experiences from this exciting journey of being a .NET developer.