Partager via


CA1721: Property names should not match get methods

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
TypeName PropertyNamesShouldNotMatchGetMethods
CheckId CA1721
Category Microsoft.Naming
Breaking Change Breaking

Cause

The name of a public or protected member starts with 'Get' and otherwise matches the name of a public or protected property. For example, a type that contains a method that is named 'GetColor' and a property that is named 'Color' violates this rule.

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 reduces the time that is 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 IExtenderProvider interface.

Example

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

using System;

namespace NamingLibrary
{
    public class Test
    {
        public DateTime Date
        {
            get { return DateTime.Today; }
        }
         // Violates rule: PropertyNamesShouldNotMatchGetMethods.
        public string GetDate()
        {
            return this.Date.ToString();
        }
    }
}
Imports System

Namespace NamingLibrary

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

CA1024: Use properties where appropriate