Skip to content

Property Accessor

Accessor should be refer to proper field

What does this mean ?

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.

What can happen ?

In property and indexer set methods, and in event add and remove methods, the implicit value parameter holds the value the accessor was called with. Not using the value means that the accessor ignores the caller's intent which could cause unexpected results at runtime.

Recommendation

One basic pattern for implementing a property involves using a private backing field for setting and retrieving the property value. The get accessor returns the value of the private field, and the set accessor may perform some data validation before assigning a value to the private field.

Sample Code

Vulnerable :

private int count;
public int Count
{
  get { return count; }
  set { count = 42; } // Noncompliant
}

Non Vulnerable :

private int count;
public int Count
{
  get { return count; }
  set { count = value; }
}

References