C#idiomatic C#
ca1721fundamentals/code-analysis/quality-rules

Code analysis

CA1721: Property names should not match get methods

Learn about code analysis rule CA1721: Property names should not match get methods

CA1721 Naming enabled by default in .NET 10: No
Fix is breaking or non-breaking BreakingApplicable languages C# and Visual Basic

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 System.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 System.ComponentModel.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.

csharp
#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.

ini
[*.{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, by setting the api_surface option. 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:

ini
dotnet_code_quality.CAXXXX.api_surface = private, internal

Example

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

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1721.cssnippet1

csharp
using System;

namespace ca1721
{
    //<snippet1>
    public class Test
    {
        public DateTime Date
        {
            get { return DateTime.Today; }
        }

        // Violates rule: PropertyNamesShouldNotMatchGetMethods.
        public string GetDate()
        {
            return this.Date.ToString();
        }
    }
    //</snippet1>
}

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1721-property-names-should-not-match-get-methods_1.vb

vb
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