Quantcast
Channel: The Daily Parker - General
Viewing all articles
Browse latest Browse all 2429

N is for Namespace

$
0
0

Blogging A to ZDay 14 of the Blogging A-to-Z challenge brings us to namespaces.

Simply put, a namespace puts logical scope around a group of types. In .NET and in other languages, types typically belong to namespaces two or three levels down.

Look at the sample code for this series. You'll notice that all of the types have a scope around them something like this:

namespace InnerDrive.Application.Module
{
}

(In some languages it's customary to use the complete domain name of the organization creating the code as part of the namespace and to use alternate letter cases. If I were writing Java, for example, that would look like com.inner-drive.application.module.)

Every type defined in the namespace belongs to only that namespace. If I defined a type in the example namespace above called Foo, the fully-qualified type name would be InnerDrive.Application.Module.Foo. Because using FQTNs requires a lot of typing and makes code harder to read, .NET gives you another use of the namespace keyword:

namespace InnerDrive.Application.Module;

namespace InnerDrive.Application.OtherModule
{
	public class Bar
	{
		public void Initialize() 
		{
			// var foo = new InnerDrive.Application.Module.Foo() is not required
			var foo = new Foo();
			foo.Start();
		}
	}
}

Also, that Bar class belongs only to the InnerDrive.Application.OtherModule namespace, so another developer could create another Bar class in her own namespace without needing to worry about mine.


Viewing all articles
Browse latest Browse all 2429

Trending Articles