Share via


CA1020: Avoid namespaces with few types

TypeName

AvoidNamespacesWithFewTypes

CheckId

CA1020

Category

Microsoft.Design

Breaking Change

Breaking

Cause

A namespace other than the global namespace contains fewer than five types.

Rule Description

Make sure that each of your namespaces has a logical organization, and that a valid reason exists to put types in a sparsely populated namespace. Namespaces should contain types that are used together in most scenarios. When their applications are mutually exclusive, types should be located in separate namespaces. For example, the System.Web.UI namespace contains types that are used in Web applications, and the System.Windows.Forms namespace contains types that are used in Windows-based applications. Even though both namespaces have types that control aspects of the user interface, these types are not designed for use in the same application. Therefore, they are located in separate namespaces. Careful namespace organization can also be helpful because it increases the discoverability of a feature. By examining the namespace hierarchy, library consumers should be able to locate the types that implement a feature.

Note

Design-time types and permissions should not be merged into other namespaces to comply with this guideline. These types belong in their own namespaces below your main namespace, and the namespaces should end in .Design and .Permissions, respectively.

How to Fix Violations

To fix a violation of this rule, try to combine namespaces that contain just a few types into a single namespace.

When to Suppress Warnings

It is safe to suppress a warning from this rule when the namespace does not contain types that are used with the types in your other namespaces.