C# Namespaces Tutorial

In this tutorial we learn about namespaces in C#. How they group code and help our applications avoid class and class member name clashing.

What is a namespace

A namespace is an element that’s designed to help us organize our programs and avoid name clashing.

In a typical application, we separate our code into smaller sections of logic, such as classes. A namespace groups classes together, or simply encloses a single class.

Everything inside a namespace will be of that namespace. If a project contains two classes with the same name, but different namespaces, their names will not clash.

How to declare a namespace

Before we continue, let’s create a new C# file.

  1. In the Solution Explorer, right-click on HelloWorld. From the flyout menu select Add > Class.
  2. Name the new class Messages.cs and click Add.

You should now see two classes in your solution, Program.cs and Messages.cs.

If it didn’t open automatically, double-click Messages.cs to open it in the Code Editor.

By default, our new class will be of the HelloWorld namespace, but we want a different namespace for this lesson. Go ahead and delete all the code inside the class.

To declare a namespace we write the keyword namespace , followed by the name we want to give it, and then open and close curly braces { } .

Syntax: declare a namespace
namespace Identifier
{

}

Everything between the curly braces is part of that namespace.

Example: Messages.cs
namespace ConsoleMessages
{
    class Messages
    {
        public void Hello()
        {
            Console.WriteLine("Hello there");
        }
        public void Bye()
        {
            Console.WriteLine("Goodbye");
        }
    }
}

In the example above, we declared our own namespace called ConsoleMessages. Inside it we declared our Messages class again, and wrote two functions for it.

How to use a namespace

To have access to the content inside a namespace, we use the using directive at the top of the page that we want to use it in.

For example, the Program class is our main class and runs the application. If we want to use the ConsoleMessages namespace inside the Program class, we need to specify the using directive at the top of the Program.cs file.

Syntax: using a namespace
using System;
using CustomNamespace;

We can think of the using directive as an import or include statement. It makes everything inside the namespace available to us for use in that document.

Example: Program.cs
using System;
using ConsoleMessages;

namespace Namespaces
{
    class Program
    {
        static void Main(string[] args)
        {
            Messages msg = new Messages();

            msg.Hello();
            Console.ReadLine();
        }
    }
}

In the example above, we specify the using ConsoleMessages directive at the top of the Program.cs page. This allows us to have access to the Messages class, and the Hello() function, which we use.

If we run the application now, it will print “Hello there”, from the ConsoleMessages namespace, to the console.

Going back to the Messages.cs file, we’ll see that the word Console is underlined in red. This is because there is a potential error.

The Console class lives inside the System namespace, just like our Messages class lives inside the ConsoleMessages namespace. So, in order for us to have access to Console.WriteLine(), we need to have the using System directive in the file.

But, because we already have the System namespace available in the Program.cs, we don’t need it in Messages.cs.

When a class is not inside of a namespace, it will automatically be of the global namespace. This means that custom namespaces are not necessary. However, we recommend using namespaces, even when learning, for convenience and efficiency.

How to access namespace content without using the directive

We don’t have to include the using directive to have access to a namespace. We can access the content inside a namespace with dot notation.

First, we write the name of the namespace, followed by the dot operator. Then, we specify the name of whatever we want to access inside that namespace.

In this case we want to access the Hello() function, inside the Messages class, inside the ConsoleMessages namespace.

Syntax:
Namespace_name.Class_name.Member_name;

As an example, let’s consider the Console.WriteLine() function. Writeline() is a function inside the Console class, which is inside the System namespace.

When we want to access it directly without the using directive, we would write the names separated by the dot operator.

Example: system namespace direct access
System.Console.WriteLine("Hello World");
Example: Program.cs
using System;

namespace Namespaces
{
    class Program
    {
        static void Main(string[] args)
        {
            ConsoleMessages.Messages msg = new ConsoleMessages.Messages();

            msg.Hello();
            Console.ReadLine();
        }
    }
}

In the example above we don’t use the using directive at the top of the page for the ConsoleMessages namespace.

In the Main() function of our program we go directly into the ConsoleMessages namespace when we instantiate the class.

Statement:
ConsoleMessages.Message msg = new ConsoleMessages.Message();

Summary: Points to remember

  • A namespace groups classes to organize our projects and avoid name clashes.
  • For efficiency, we use the using directive to have access to a namespace’s contents.
  • We can access namespace content directly with dot notation.