CA1721: Property names should not match get methods

Value
Rule ID CA1721
Category Naming
Fix is breaking or non-breaking Breaking

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.

Note

This warning may be excluded if the "Get" method is caused by implementing the IExtenderProvider interface.

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, or for all rules in this category (Naming). 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