Total .NET Analyzer Rule Documentation |
|
Avoid using write-only properties.
Remarks
.NET allows you to create write-only properties, which can be stored but not exposed. A property is write-only if it uses the Set property accessor without the Get accessor. For instance, the following property is write-only, and can never be read:
' VB
WriteOnly Property NewCustomerID() As String
Set(ByVal value As String)
' some code here
End Set
End Property
// C#
public string NewCustomerID
{
set
{
// some code here
}
}
In general, write-only properties are not appropriate, since they do not allow data to be retrieved once it is stored. Most properties should allow read/write access or read-only access.
Resolution
Review properties that are write-only to ensure that they do not need to be exposed. If you need to expose the property, you should add a Get method to the property procedure. The following code example shows the above code modified to be read/write:
' VB
Public Property NewCustomerID() As String
Get
' some code here
End Get
Set(ByVal value As String)
' some code here
End Set
End Property
// C#
public string NewCustomerID
{
get
{
// some code here
}
set
{
// some code here
}
}
To make the property read-only, remove the Set statement.
See Also
Property Usage Guidelines
VB Properties and Property Procedures
C# Properties Tutorial