Nested class members should not mask outer class' static members

Rule ND2022 Naming Conventions

The C# compiler lets nested class members share names with static members of the outer class, but this is discouraged: it can be unclear which member the inner class code references.

How to fix

Give inner class members unique and descriptive names.

CQLinq source

This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.

// <Name>Nested class members should not mask outer class' static members</Name>
// <Id>ND2022:NestedClassMembersShouldNotMaskOutClassStaticMembers</Id>

warnif count > 0 
from t in JustMyCode.Types
where t.IsNested && t.ParentType != null
let parentTypesMemberNames = t.ParentType.Members
  .Where(pm => pm.IsStatic)
  .Select(pm => pm.Name).ToHashSetEx()
from m in t.Members

// Don't warn for these methods and backing fields
where !(m.IsMethod && 
   (m.AsMethod.IsConstructor   || m.AsMethod.IsClassConstructor ||
    m.AsMethod.IsPropertyGetter|| m.AsMethod.IsPropertySetter   ||
    m.Name == "Dispose()" ||
    m.Name.EndsWith("GetEnumerator()"))) &&
      !(m.IsField && m.Name.Contains("<"))
   && parentTypesMemberNames.Contains(m.Name)

select new { 
  m, 
  isOverlapping = t.ParentType.Members.First(pm => pm.Name == m.Name),
  declaredIn = t.ParentType,
  Debt = 2.ToMinutes().ToDebt(),
  Severity = Severity.Medium
}

//<Expl>
// The {0} should be renamed because it masks the {1} declared 
// in the outer {2}.
//</Expl>

//<Desc>
// The C# compiler lets nested class members share names with static members
// of the outer class, but this is discouraged: it can be unclear which
// member the inner class code references.
//</Desc>

//<HowToFix>
// Give inner class members unique and descriptive names.
//</HowToFix>

Naming Conventions: all 23 items