Total .NET Analyzer Rule Documentation  

GetPropertyCollision Rule

Avoid using member names that are identical to a property's Get method.

Remarks

Avoid using member names that are identical to a property's Get method. Although the compiler allows members and Get methods to be named identically, doing this creates code that is difficult to understand and maintain. Additionally, identically named members and Get methods may indicate deeper logic problems in the application.

For instance, the following code contains a subroutine named "GetNewCustomerID" and a property named "NewPropertyID":


' VB
Sub GetNewCustomerID()
' some code here...
End Sub

Public Property NewCustomerID() As String
Get
' some code here
End Get

Set(ByVal value As String)
' some code here
End Set
End Property

// C#
static void GetNewCustomerID()
{
// some code here
}

public string NewCustomerID
{
get
{
// some code here
}

set
{
// some code here
}
}


This may cause confusion in code reviews in determining whether the subroutine and the Get method perform identical tasks. If they perform the same function, then the subroutine may be unnecessary. If they do not perform the same function, then the subroutine's name is misleading.

Resolution

Examine identically named members and Get methods to determine whether they perform the same function. If they perform the same function, you may be able to streamline the code by removing the member body. If they do not perform the function, you should rename the member to be unique and descriptive. For instance, the code example above could be revised to the following:

' VB
Sub GenerateNewCustomerID()
' some code here...
End Sub

Public Property NewCustomerID() As String
Get
' some code here
End Get

Set(ByVal value As String)
' some code here
End Set
End Property

// C#
static void GetNewCustomerID()
{
// some code here
}

public string NewCustomerID
{
get
{
// some code here
}

set
{
// some code here
}
}

See Also