The C# ‘using’ Directive

A namespace is a container for a set of related objects. For example, the System namespace, which is part of the .NET framework, contains commonly used value and reference datatypes, so that variables of different types can be defined, such as, integer and string variables. The System namespace also deals with events and event handlers, interfaces, attributes, and processing exceptions.

As well as the namespaces included in the .NET framework, namespaces can also be user defined.

The ‘using’ directive allows the use of the contents of a namespace without, for example, having to include the namespace in a variable declaration. They are placed at the top of a C# script. Using the System namespace mentioned above as an example, the using directive would be as follows.

using System;

Including this directive means that, for example, a string variable can be defined as follows.

string message = "Hello World";

Without the inclusion of the directive, the variable declaration would look as follows.

System.String message = "Hello World";

When creating a C# based script in Visual Studio, Microsoft’s own development environment, it will include a number of ‘using’ directives by default at the top of the script. The System ‘using’ directive is one that is included by default.