Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Variable names should not match field names

Variable names should not match field names







Breaking Change


An instance method declares a parameter or a local variable whose name matches an instance field of the declaring type. To catch local variables that violate the rule, the tested assembly must be built with debugging information and the associated program database (.pdb) file must be available.

When the name of an instance field matches a parameter or a local variable name, the instance field is accessed using the this (Me in Visual Basic) keyword when inside the method body. While maintaining code, it is easy to forget this difference and assume that the parameter/local variable refers to the instance field, leading to errors. This is true especially for lengthy method bodies.

To fix a violation of this rule, rename either the parameter/variable or the field. Note that it is a breaking change to rename an externally visible field or a parameter in an externally visible method.

Do not exclude a warning from this rule.

The following example shows two violations of the rule.

using System;

namespace MaintainabilityLibrary
   class MatchingNames
      int someField;
      void SomeMethodOne(int someField) {}
      void SomeMethodTwo()
         int someField;

Community Additions

© 2015 Microsoft