C#: Namespaces

Author: Ronald S. Holland
Total Application Works


To contact us

HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#

© 2002 - 2014 All Rights Reserved Total Application Works

  • Tell a friend about this site (copy and paste the following HTML page into a file (e.g., using Notepad).)
    <title>Example of a link </title>
    <a href=http://sumtotalz.com/TotalAppsWorks/CSharp/CS_Namespace.html">
    Namespace </a>

Table of Contents


This tutorial is under development. This tutorial introduces you to C# Namespaces.  Our objectives are as follows:

  • Understand what a Namespace is used for.
  • Learn how to implement the using directive.
  • Learn to use alias directive.
  • Understand what are namespace members.

Introduction to C#

The purpose of this tutorial is to introduce you to the basics of C# ("C sharp") namespaces. Even if you've never programmed in a language similar to C/C++ or Java before, this tutorial should be very easy to follow and understand. It assumes that you have some basic exposure to programming (some object-oriented experience would be helpful, but is not necessary) and you have some kind of C# compiler (if you would like to run the examples). First, you should know that C# is a language designed by Microsoft. It is intended to combine the most significant parts of C/C++ along with the productivity of a 4GL, such as Visual Basic. If you do not have a C# compiler, instructions on how to obtain one will be described in the next section.

What is a Namespace?

Often when programmers create reusable components, the components they create have names that result in naming collisions. No two classes in the same namespace can have the same name. In general, a namespace is a container for a set of identifiers (also known as symbols, names). When these symbol names, variable names, or classes have the same name, it is called a name collision. Figure 1 contains some examples of name collisions and an example of method overloading.


      int a = 5 ;  // two variables with the same
      int a = 5 ;  // 'int a' : redefinition  error

      class AnyName {  // Method overloading 
         // first version, takes one integer
         public int Square ( int x ) {
            return x * x;

         // second version, takes one double
         public double Square ( double y ) {
            return y * y;
      class AnyName {  // error C2011: 'AnyName' : 
      }                // 'class' type redefinition
Figure 1: Naming collisions

We believe in templates, so in the next section, we will explore building a namespace template.

Namespace Template
A namespace template gives you the ability to layout a program or module to review the high-level design.

// A namespace template in C#.

using System;

// Namespace Declaration
namespace MyTemplate
    // Program start class description for MyClass.

   class MyClass
        // Main begins program execution.
      static void Main( string[] args )
         // TODO: Add code to start application here
            // Write to console
         Console.WriteLine( "Welcome to C# Programming!" );
            // Read from console - causes program to pause
         Console.ReadLine( );

Figure 2: Namespace Template

You will need a C# compiler to compile and run c# examples. See the next section to find out how to obtain a free c# compiler.

Free Compilers

If you don't have a c# compiler, you can download a free copy at Microsoft's 2013 Visual Studio Downloads Page or Microsoft's 2013 Visual Studio Downloads Page. You may be able to download a copy in 5-10 minutes, if you have access to a broadband line at a public college or a public library, assuming a line is available. You may be able to download a copy in 15-20 minutes, if you have access to a broadband internet provider. You can follow the steps below:

  1. Download a free copy of VC++ at Microsoft's Visual Studio Downloads Page.
    • This copy is in image file format (ISO images).
    • I used this option: All - Offline Install ISO image file. With this option, you get the following:
      • Visual Basic 2013 Express Edition
      • Visual C# 2013 Express Edition
      • Visual C++ 2013 Express Edition
      • Visual Web Developer 2013 Express Edition
  2. Download a free copy of VCdControlTool. It only works with ISO images. Installation instructions:
    1. Copy VCdRom.sys to your WINDOWS\system32\drivers folder.
    2. Execute VCdControlTool.exe
    3. Click "Driver control"
    4. If the "Install Driver" button is available, click it. Navigate to theWINDOWS\system32\drivers folder, select VCdRom.sys, and click Open.
    5. Click "Start"
    6. Click OK
    7. Click "Add Drive" to add a drive to the drive list. Ensure that the drive added is not a local drive. If it is, continue to click "Add Drive" until an unused drive letter is available.
    8. Select an unused drive letter from the drive list and click "Mount".
    9. Navigate to the image file (VS2010xpressWithSP1ENUX1504728.iso or VS2010Express1.iso), select it, and click "OK". UNC naming conventions should not be used; however, mapped network drives should be OK.
  3. Next use Windows Explorer to navigate to the Virtual drive you mounted. You will see the following folders:
    • Include
    • VBExpress
    • VCExpress
    • VCSExpress
    • VWDExpress
  4. Use the setup application in each folder to install the desired components.
  5. You are ready to start programming.
Next, we will explore the concept of namespaces in more detail.


We use namespaces in C# to aid in organizing our programs at the highest logical level. They classify and present programming elements that are exposed to other programs and applications. They also provide assistance in avoiding name clashes between two sets of code. Within a namespace, we can declare:

  • another namespace,
  • a class,
  • an interface,
  • a struct,
  • an enum or
  • a delegate.
We cannot define items, like, properties, variables and events in namespaces. Items, such as these, must be declared within containers such as structures or classes.

Using Namespaces in the code you develop is a good habit because it may save you from problems later when you want to reuse some other classes. For example, if you created a class that contain sqrt and Pow methods, and you include another class that contain the same method names. The new mthods that you include produce different results than the methods you wrote.

When you view Figure 1 and Figure2, It is difficult to see how you can ge a name collision. But consider the following:
  • Your're working on a large project consisting of:
    • Several million lines of code;
    • Several 3rd party developers;
    • Code being devekoped at different sites.
  • Different development protocols used at the various sites.


In summary, you know what a namespace is and you can declare your own namespaces. If you don't want to type a fully qualified name, you know how to implement the using directive. When you want to shorten a long namespace declaration, you can use the alias directive. Also, you have been introduced to some of the other namespace members in addition to the class type.



    Computer scientists usually credit the Structured program theorem to a 1966 paper by Corrado Böhm and Giuseppe Jacopini.

Boolean expression

    is a programming language expression that when evaluated produces a Boolean value, i.e. one that is either true or false. A Boolean expression may be composed of a combination of:
    1. the Boolean constants true or false;
    2. Boolean-typed variables;
    3. Boolean-valued operators; and
    4. Boolean-valued functions.

conditional operator ? :

    The conditional operator ? : is a ternary operator (it takes three operands). It is of the form:
    (condition ? expression 1 : expression 2 ) 

  • If the condition evaluates to true then expression 1 is executed; otherwise, expression 2 is executed.

control structure

    allows for a change of linear sequence/flow of program execution, based on some condition evaluating to true.


    A flowchart is a method of diagraming the layout and/or flow of a program or algorith. This flow is shown using figures of various kinds, and their order by connecting them with arrows.

method overloading

    overloading is what happens when you have two methods with the same name but different signatures. Overload resolution is a compile-time mechanism where the method selected is based on the best function member to invoke given an argument list/signature and a set of candidate function members.


    is a compact and informal high-level design description (usually written in a spoken language) of a computer programming algorithm that adheres to the structural conventions of a programming language. Pseudocode is intended to be read by humans rather than by machine.


    in Computer science, a template can be pseudocode or an executable outline that is used as a tool for generic programming to outline the program.

HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript ] C#

To contact us

© 2002 - 2014 All Rights Reserved Total Application Works