Total .NET Analyzer Rule Documentation  

TypeNotInNamespace Rule

Avoid defining type names outside of a namespace.

Remarks

.NET allows you to define types outside of a namespace, but this is generally not a good idea. Defining types within a namespace makes code easier to read and understand, and it helps minimize the risk of duplicating type names and definitions.

The following code shows an example of a duplicate type definition caused by having the definition outside of the namespace:


' VB
Public Class CreateDocument
' Code here
End Class

Namespace ClientDocs
Public Class CreateDocument
' Code here
End Class
End Namespace

// C#
public class CreateDocument
{
// Code here
}

namespace ClientDocs
{
public class CreateDocument
{
// Code here
}
}


This code is difficult to read, and it would be easy to inadvertently call the incorrect class.

Resolution

Move the type definition to the namespace. For instance, you could modify the above code to:

' VB
Namespace ClientDocs

Public Class CreateCoverLetter
' Code here
End Class

Public Class CreateDesignDoc
' Code here
End Class

End Namespace

// C#
namespace ClientDocs
{
public class CreateCoverLetter
{
// Code here
}
public class CreateDesignDoc
{
// Code here
}
}

See Also

Type Definitions