C# Coding Solutions—Namespaces
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
Namespaces
Millions of lines of source code have been written using .NET. Therefore it is important to choose identifiers well to avoid using an identifier that somebody has already used for another functionality.
Consider the following namespace:
namespace Devspace.Commons.Cache { }
The namespace can be split into three pieces that are defined as follows:
- Devspace. This is the entity that creates the application, assembly, source code, etc. This could be a corporation or an organization. In most cases you should use the unique Internet domain identifier associated with your company. Be careful when using corporate identifiers that are not Internet-based, as multiple companies in multiple countries may have the same identifier.
- Commons. This is a project’s main identifier. Typically it is either the overall project name or an important part of a project.
- Cache. This is the subsystem project identifier. Multiple identifiers can all relate to the subsystem.
Using three unique identification chunks creates a unique namespace. Don’t worry if a namespace becomes long; a well-named namespace makes the code consistent and easy to identify. Naming the subsections underneath a project is fuzzier. Here is how the examples for this book are laid out:
-
Devspace.HowToCodeDotNet01: This defines the root namespace for the examples in this book. There are two pieces to the root namespace because the book is called How to Code .NET, and this is Volume 1.
-
-
Devspace.HowToCodeDotNet01. MakingToStringGenerateStructuredOutput: The identifierMakingToStringGenerateStructuredOutputreferences the concepts of a solution that is explained in this book. The solution identifier is very long so that the reader of the namespace does not have to guess what the short forms mean. In general, avoid using short forms because they lead to guessing, and often incorrect guessing.
-
-
Devspace.HowToCodeDotNet01.VersioningAssemblies.Assembly: This is an embedded namespace (Assembly) within the solution namespace identifier (VersioningAssemblies). The embedded namespace identifies a piece of functionality that belongs to the solution. The solution is an assembly used to illustrate how versioning works in .NET.
-
Note Microsoft does not follow exact naming conventions for many packages (such as System.Windows, which could have been written as Microsoft.Windows because after all, there are multiple GUI packages on .NET), and you should not follow suit. Microsoft created the .NET Framework and has identified the common namespaces that most .NET applications will use.
|
In all three examples the namespace is relatively long so that there is no conflict with other namespaces. I could have dropped the Devspace.HowToCodeDotNet01 identifiers because my examples likely will not be used anywhere but in my examples. However, if I were to write another .NET coding book a conflict could arise. Additionally, I often end up using one-time sample code in production, and vice versa.
|

