Edit

Share via


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)
    {
    }
}
' Inside a project with <OutputKind>Exe</OutputKind>.
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)
    {
    }
}
' Inside a project with <OutputKind>Exe</OutputKind>.
Friend Class Program
    Public Shared Sub Main(args As string())
    End Sub
End Class

(For more information about the output type of a project, see the "Output type" section of .NET Project Designer.)

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.

Configure code to analyze

You can configure which output assembly kinds to apply this rule to. For example, to only apply this rule to code that produces a console application or a dynamically linked library (that is, not a UI app), add the following key-value pair to an .editorconfig file in your project:

dotnet_code_quality.CA1515.output_kind = ConsoleApplication, DynamicallyLinkedLibrary

For more information, see output_kind.