All rules
CA1034Design Enabled by default: No

Nested types should not be visible

Nested types should not be visible

Microsoft docs

Description

A nested type is a type declared within the scope of another type. Nested types are useful for encapsulating private implementation details of the containing type. Used for this purpose, nested types should not be externally visible.

Do not use externally visible nested types for logical grouping or to avoid name collisions; instead, use namespaces.

Nested types include the notion of member accessibility, which some programmers do not understand clearly.

Protected types can be used in subclasses and nested types in advance customization scenarios.

Cause

An externally visible type contains an externally visible type declaration. Nested enumerations, protected types, and builder patterns are exempt from this rule.

How to fix violations

If you do not intend the nested type to be externally visible, change the type's accessibility. Otherwise, remove the nested type from its parent. If the purpose of the nesting is to categorize the nested type, use a namespace to create the hierarchy instead.

Example

public class ParentType
{
    public class NestedType
    {
        public NestedType()
        {
        }
    }

    public ParentType()
    {
        NestedType nt = new NestedType();
    }
}

When to suppress

Do not suppress a warning from this rule.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0