Total .NET Analyzer Rule Documentation  

SetPropertyCollision Rule

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

Remarks

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

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


' VB
Sub SetNewCustomerID()
' 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 SetNewCustomerID()
{
// 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 Set 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 Set 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 CreateNewCustomerID()
' 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 SetNewCustomerID()
{
// some code here
}

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

set
{
// some code here
}
}

See Also