CA1050: Declare types in namespaces

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at CA1050: Declare types in namespaces.

TypeName|DeclareTypesInNamespaces|
|CheckId|CA1050|
|Category|Microsoft.Design|
|Breaking Change|Breaking|

A public or protected type is defined outside the scope of a named namespace.

Types are declared in namespaces to prevent name collisions, and as a way to organize related types in an object hierarchy. Types that are outside any named namespace are in a global namespace that cannot be referenced in code.

To fix a violation of this rule, place the type in a namespace.

Although you never have to suppress a warning from this rule, it is safe to do this when the assembly will never be used together with other assemblies.

The following example shows a library that has a type incorrectly declared outside a namespace, and a type that has the same name declared in a namespace.

No code example is currently available or this language may not be supported.

The following application uses the library that was defined previously. Note that the type that is declared outside a namespace is created when the name Test is not qualified by a namespace. Note also that to access the Test type in Goodspace, the namespace name is required.

No code example is currently available or this language may not be supported.

Show: