C# Tutorial – Namespace and using directive

global alias

facebooktwittergoogle_pluslinkedinmail

It’s normal, that in big programs there will be many classes. Having them in flat hierarchy would cause a headache. That’s why C# has a concept of namespaces. They allow to organize classes in a hierarchical way. Good example is .NET Framework.

Let’s have a look at code sample below.

System.Console.WriteLine("Hello, World!");

Console class is in System namespace.

  • System is a namespace
  • Console is a class
  • WriteLine is a method

Next example:

System.Collections.SortedList list;

It’s a declaration of list variable of type System.Collections.SortedList, where:

  • System.Collections is a namespace
  • SortedList is a class

More on Namespaces Programming Guide.

 

Using directive

The using directive has one very important usage. It allows to use type from namespace and one will not have to qualify the full name. For example, instead of using following code:

System.Console.WriteLine("Hello, World!");

You can add using directive at the begging of the file:

using System;

and type following statement in code:

Console.WriteLine("Hello, World!");

Visual Studio adds default using statements in every new C# file. That’s why in C# Hello World application was only class name used instead of full name.

There is second usage of using directive – to create using statements in code (will write a post about it later).

More on using Directive in MSDN documentation.

 

Namespace aliases

The using directive can be also used to create namespace aliases. For example:

using col = System.Collections;

To reference aliased namespace you need to use ::

col::SortedList list2;

You can also create alias to a class.

using console = System.Console;

Class alias should be used with .  instead of ::

console.WriteLine();

 

Global namespace alias

Let’s assume you have System class defined in your code:

namespace MyFirstApplication
{
    class Program
    {
        public class System { }

        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello, World!"); // Error
        }
    }
}

The code won’t compile as System namespace is hidden by System class declaration. So, is there a way to call hidden namespace? Yes, there is special global alias.

namespace MyFirstApplication
{
    class Program
    {
        public class System { }

        static void Main(string[] args)
        {
            global::System.Console.WriteLine("Hello, World!"); // No error this time :)
        }
    }
}

But what will happen if one would like to make it’s life more difficult and will define own global alias?

using global = System;

It’s pointless. As it will generate a warning saying that global::  will always reference global namespace and not the alias.

Let’s consider next example:

using global = System.Console;

This time you can use the alias as it references a class and is used with .  instead of ::

global.WriteLine("Test");

But anyway the alias declaration will generate the same warning. So just be aware of that possibility but please never do it yourself – in general, it’s just a bad practice.

MSDN documentation about global namespace usage.

facebooktwittergoogle_pluslinkedinmail

Leave a comment

Your email address will not be published. Required fields are marked *