Skip to content

Property Accessor

The accessor should point to the appropriate field.

What does this mean ?

A property is a member that allows you to read, write, or compute the value of a private field with ease. Properties are special procedures known as accessors that may be utilized as if they were public data members. This allows data to be quickly accessed while also promoting method safety and flexibility.

What can happen ?

The implicit value parameter in property and indexer set methods, as well as event add and delete methods, contains the value the accessor was called with. If the value isn't used, the accessor will ignore the caller's purpose, which might result in unexpected runtime outcomes.

Recommendation

Using a private backing field to set and get the property value is a common approach for constructing a property. The set accessor may do some data validation before assigning a value to the private field, while the get accessor retrieves the value of 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