Редагувати

Поділитися через


CA1724: Type names should not match namespaces

Property Value
Rule ID CA1724
Title Type names should not match namespaces
Category Naming
Fix is breaking or non-breaking Breaking
Enabled by default in .NET 9 No

Cause

A type name matches a referenced namespace name that has one or more externally visible types. The name comparison is case-insensitive.

Rule description

User-created type names should not match the names of referenced namespaces that have externally visible types. Violating this rule can reduce the usability of your library.

How to fix violations

Rename the type such that it doesn't match the name of a referenced namespace that has externally visible types.

When to suppress warnings

For new development, no known scenarios occur where you must suppress a warning from this rule. Before you suppress the warning, carefully consider how the users of your library might be confused by the matching name. For shipping libraries, you might have to suppress a warning from this rule.

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 CA1724
// The code that's violating the rule is on this line.
#pragma warning restore CA1724

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

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

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

Example

namespace MyNamespace
{
    // This class violates the rule
    public class System
    {
    }
}