Edit

Share via


CA1721: Property names should not match get methods

Property Value
Rule ID CA1721
Title Property names should not match get methods
Category Naming
Fix is breaking or non-breaking Breaking
Enabled by default in .NET 8 No

Cause

The name of a member starts with 'Get' and otherwise matches the name of a property. For example, a type that contains a method that's named 'GetColor' and a property that's named 'Color' cause a rule violation. This rule will not fire if either the property or the method is marked with the ObsoleteAttribute.

By default, this rule only looks at externally visible members and properties, but this is configurable.

Rule description

"Get" methods and properties should have names that clearly distinguish their function.

Naming conventions provide a common look for libraries that target the common language runtime. This consistency reduces the time that's required to learn a new software library and increases customer confidence that the library was developed by someone who has expertise in developing managed code.

How to fix violations

Change the name so that it does not match the name of a method that is prefixed with 'Get'.

When to suppress warnings

Do not suppress a warning from this rule. One exception to that rule is if the "Get" method is caused by implementing the IExtenderProvider interface.

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

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

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

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

Configure code to analyze

Use the following option to configure which parts of your codebase to run this rule on.

You can configure this option for just this rule, for all rules it applies to, or for all rules in this category (Naming) that it applies to. For more information, see Code quality rule configuration options.

Include specific API surfaces

You can configure which parts of your codebase to run this rule on, based on their accessibility. For example, to specify that the rule should run only against the non-public API surface, add the following key-value pair to an .editorconfig file in your project:

dotnet_code_quality.CAXXXX.api_surface = private, internal

Example

The following example contains a method and property that violate this rule.

public class Test
{
    public DateTime Date
    {
        get { return DateTime.Today; }
    }

    // Violates rule: PropertyNamesShouldNotMatchGetMethods.
    public string GetDate()
    {
        return this.Date.ToString();
    }
}
Imports System

Namespace ca1721

    Public Class Test

        Public ReadOnly Property [Date]() As DateTime
            Get
                Return DateTime.Today
            End Get
        End Property

        ' Violates rule: PropertyNamesShouldNotMatchGetMethods.
        Public Function GetDate() As String
            Return Me.Date.ToString()
        End Function

    End Class

End Namespace