Muokkaa

Jaa


CA1515: Consider making public types internal

Property Value
Rule ID CA1515
Title Consider making public types internal
Category Maintainability
Fix is breaking or non-breaking Non-breaking
Enabled by default in .NET 9 No

Cause

A type inside an executable assembly is declared as public.

Rule description

Unlike a class library, an application's API isn't typically referenced publicly, so types can be marked internal.

Internal types, in turn, can benefit from various code analyzers that target non-public APIs.

How to fix violations

Mark the type as internal.

Example

The following code snippet shows a violation of CA1515:

// Inside a project with <OutputKind>Exe</OutputKind>
public class Program
{
    public static void Main(string[] args)
    {
    }
}
Public Class Program
    Public Shared Sub Main(args As string())
    End Sub
End Class

The following code snippet fixes the violation:

// Inside a project with <OutputKind>Exe</OutputKind>
internal class Program
{
    public static void Main(string[] args)
    {
    }
}
Friend Class Program
    Public Shared Sub Main(args As string())
    End Sub
End Class

When to suppress warnings

It's safe to suppress a violation of this rule if you're not concerned about the maintainability of your code.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable CA1515
// The code that's violating the rule is on this line.
#pragma warning restore CA1515

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

[*.{cs,vb}]
dotnet_diagnostic.CA1515.severity = none

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