Edit

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

Property Value
Rule ID CA2266
Title File-based program entry point should start with #!
Category Usage
Fix is breaking or non-breaking Non-breaking
Enabled by default in .NET 10 As warning
Applicable 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.

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:

#:include "helpers.cs"

Console.WriteLine(GetMessage());

In helpers.cs:

string GetMessage() => "Hello";

The following code snippet fixes the violation.

In app.cs:

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

Console.WriteLine(GetMessage());

In helpers.cs:

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.

#:property NoWarn=$(NoWarn);CA2266

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

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

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

See also

File-based apps