← back to index

S1117 — Local variables should not shadow class fields or properties

Language: C#  |  Type: CODE_SMELL  |  Severity: Major

Tags: suspicious, pitfall

Why is this an issue?

Shadowing occurs when a local variable has the same name as a variable, field, or property in an outer scope.

This can lead to three main problems:

To avoid these problems, rename the shadowing, shadowed, or both variables/fields/properties to accurately represent their purpose with unique and meaningful names. It improves clarity and allows reasoning locally about the code without considering other software parts.

This rule focuses on variables shadowing fields or properties.

Noncompliant code example

class Foo
{
  public int myField;
  public int MyProperty { get; set; }

  public void DoSomething()
  {
    int myField = 0;    // Noncompliant
    int MyProperty = 0; // Noncompliant
  }
}

Resources

Documentation

Related rules