developmentor - a developer services company				  
				      C# Tutorial
   Modules

Namespace


Goals:

  • Create top level and nested namespaces.
  • Access namespace contents using fully qualified names.
  • Employ a using directive to get shorthand access to an entire namespace.
  • Create a using alias for a namespace.
  • Create a using alias for a class.

Overview

A namespace defines a scope that can contain types and other namespaces. Partitioning code into namespaces allows related types to be grouped together into a logical unit. Grouping related classes makes life easier for users who need to navigate a large library since the types are sorted into categories. Namespaces also provide name management: two types can have the same name as long as they are in different namespaces.

To access a namespace member, user code can employ the fully qualified name. For example, to access a class named Circle in the Shapes namespace the code would be: Shapes.Circle. Typing fully qualified names quickly becomes tedious and can make code look cluttered. To help avoid the long names, programmers can employ the using keyword in several ways. First, a using directive gives short name access to the entire contents of a namespace. Second, a using alias can provide a more intuitive or concise name for a namespace. Third, a using alias can be created for a class within a namespace.

A namespace is defined with the keyword namespace and a set of curly braces. All types defined inside the curly braces become members of the namespace. The definition does not have to be contiguous; for example, namespaces with the same name defined in different files are conceptually merged together into a single namespace.

Namespaces can be nested as deeply as necessary. For example, a namespace for different types of geometric shapes could have a nested namespaces for 2D types and another one for 3D types.


part 1- Defining namespaces

Here we create namespaces for a group of classes. The goal is to practice with the syntax for defining a namespace.

steps:

  1. Put the Photo and photo Album classes in a namespace called Photography.

  2. Create a namespace called Music for music industry types. Create two nested namespaces: Works for musical works and Business for the business side of the industry. Add the music Album class to the Works namespace. Add the Agent to the Business namespace.

class Photo
{
	public string Title;
}

class Album
{
	public Photo[] Photos = new Photo[10];
}

class Album
{
	public string Artist;
	public string Title;
}

class Agent
{
	public string Name;
	public double Commission;
}

part 2- Fully qualified names

Here we access the namespace members through their fully qualified names.

steps:

  1. Create a class called NamespaceTest in the global namespace. Add a Main method to the class.

  2. Declare a Photo using only the short name. The compiler should issue an error saying that the type could not be found.

  3. Declare a variable of each of the 4 classes with the fully qualified names. There is no need to actually create or manipulate objects for this exercise, declaring references and making sure the code compiles is sufficient.


part 3- Using directive

Here we code a using directive to get shorthand access to the entire contents of a namespace.

steps:

  1. Place a using directive for the Photography namespace at the top of the file containing your test code.

  2. Use the short names to declare a Photo and an Album. Test to make sure the code compiles.

  3. Place a using directive for the Music.Works namespace at the top of the file containing your test code. We now have using directives for two namespaces which both contain an Album class. The two using directives do not cause a compilation error by themselves; however, attempting to use the short name to declare an Album will yield a compiler error since the compiler cannot know which Album class to use. Resolve the ambiguity with the fully qualified name for the class you would prefer and test that the code compiles.


part 4- Using alias

A using alias can be used to create a more convenient name for either a namespace or a class. This is especially useful for deeply nested types. In addition, an alias for a class is often used a way to resolve ambiguity when multiple using directives are present.

steps:

  1. Create an alias named Works for the Music.Works namespace. Notice that we have reused the namespace name for the alias. Reuse of the name is not required and any name could have been used. Make use of the alias to declare an Album. Test to make sure the code compiles.

  2. Create an alias named TheAgent for the Music.Business.Agent class. Make use of the alias to declare an agent. Test to make sure the code compiles.

Solutions:

This material is excerpted from the Programming C# course offered by DevelopMentor.