C#idiomatic C#
ca2266fundamentals/code-analysis/quality-rules

Code analysis

CA2266: File-based program entry point should start with `#!`

Learn about code analysis rule CA2266 - File-based program entry point should start with '#!'

CA2266 Usage enabled by default in .NET 10: As warning
Fix is breaking or non-breaking Non-breakingApplicable languages C#

Cause

Your entry point file in a multi-file file-based program doesn't start with a shebang (#!) line.

Rule description

For correct tool identification and direct shell execution support, start the entry point file with a shebang (#!) line to clearly distinguish it from files brought in with #:include or #:ref.

How to fix violations

Add a shebang line to the beginning of the entry point file, for example, #!/usr/bin/env dotnet.

Example

The following code snippet shows a violation of CA2266.

In app.cs:

csharp
#:include "helpers.cs"

Console.WriteLine(GetMessage());

In helpers.cs:

csharp
string GetMessage() => "Hello";

The following code snippet fixes the violation.

In app.cs:

csharp
#!/usr/bin/env dotnet
#:include "helpers.cs"

Console.WriteLine(GetMessage());

In helpers.cs:

csharp
string GetMessage() => "Hello";

When to suppress warnings

If you intentionally omit the shebang line and have confirmed that your tooling can still identify the entry point correctly, it is safe to suppress this warning.

Suppress a warning

For file-based apps, suppress a single violation by adding a #:property directive to the entry point file.

csharp
#:property NoWarn=$(NoWarn);CA2266

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

ini
[*.cs]
dotnet_diagnostic.CA2266.severity = none

For more information, see How to suppress code analysis warnings.

See also

File-based apps