CA1721:属性名不应与 get 方法冲突

类型名

PropertyNamesShouldNotMatchGetMethods

CheckId

CA1721

类别

Microsoft.Naming

是否重大更改

原因

公共或受保护成员的名称以“Get”开头,且其余部分与公共或受保护属性的名称匹配。例如,包含名为“GetColor”的方法和名为“Color”的属性的类型与该规则冲突。

规则说明

Get 方法和属性的名称应当能够明确表示其功能。

命名约定为所有针对公共语言运行时的库提供了通用的外观。这缩短了学习新软件库所需的时间,并使客户进一步认为该软件库是由某位具有开发托管代码专门技术的人员所开发。

如何解决冲突

更改该名称,使其不与前缀为“Get”的方法名称匹配。

何时禁止显示警告

不要禁止显示此规则发出的警告。

说明说明

如果 Get 方法是由实现 IExtenderProvider 接口引起的,则可以排除此警告。

示例

下面的示例包含与该规则冲突的方法和属性。

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
using System;

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

相关规则

CA1024:在适用处使用属性