Website development and design blog, tutorials and inspiration

Using Namespaces in C#

Using namespaces to group classes

Written By on in C# 2

Using Namespaces in C#

1,162 words, estimated reading time 6 minutes.

In this tutorial, we will have a look at Namespaces in C#. What are they? and How can we use them? Why do we use them?
C# Programming Series
  1. Introduction to Programming
  2. What is C#?
  3. Your First Console Application in C#
  4. Introducing Methods and the Main() Function in C#
  5. Introducing C# Classes and Structs
  6. C# Data Types, Variables and Casting
  7. C# Program Flow Control and Entry Points
  8. Passing Parameters to Methods and Return Values in C#
  9. C# Access Modifiers and Scope
  10. C# Interfaces and Classes
  11. Using Namespaces in C#
  12. C# Conditional Statements
  13. Looping and Iteration in C#
  14. Using Arrays and Lists in C#
  15. C# Constants and Read-Only Variables
  16. Error and Exception Handling in C#
  17. Using Recursion in C#
  18. C# Operator List
  19. Class Inheritance in C#
  20. C# Class and Method Attributes Explained
  21. C# Class Constructors and Destructors
  22. C# Generics Variables
  23. XML Serialization and Deserialization
  24. C# String Formatting Examples

In the previous tutorials we used the System namespace directive, using System; We will now look in more detail at what namespaces are and how they can be used.

The entire .Net framework is built using namespaces, the most important of which is System. A Namespace is a construct that can be used to organise sections of code, be it classes, structs, other namespaces or even entire solutions. Namespaces are used to avoid code conflicts and to group together classes of similar functionality. It is good practice to implement namespaces in your projects, as it will aid code reuse in later projects.

Standard Namespaces

In the previous tutorial we used the System namespace. Let's have a play with our Hello World application in Visual Studio. Launch Visual Studio from the Start Menu, and create a new console project (File » New » Project... » Visual C# » Windows » Console)

Delete all the code on the screen and paste in the Hello World code from before. For convenience it is listed again below.

  1. using System;
  3. class HelloWorld
  4. {
  5. static void Main()
  6. {
  7. Console.WriteLine("Hello World!");
  8. }
  9. }

Press Ctrl+Shift+B to build the project or select Build Solution from the Build menu. The output window should show:

------ Build started: Project: ConsoleApplication1, Configuration: Debug Any CPU ------
ConsoleApplication1 -> C:csharp-tutorialshello-worldinDebughello.exe
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========
This message indicates that the compile of the application was successful. Now lets remove the using directive and see what happens. 
<h2>Using Namespaces</h2>
Remove the line <code>using System;</code> and hit <strong>Ctrl+Shift+B</strong> again to build the project. You'll notice that the compiler now flags up an error.
<pre>The name 'Console' does not exist in the current context

This is because the Console object exists within the System namespace. Since we have not told the compiler where to find the Console it raises an error.

There are two methods to fix this problem. We can either add System before the Console object:

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

Or a better method would be to use the "using" directive. This is because a namespace can contain another namespace, which can contain another namespace. This will lead to a large amount of unnecessary code typed in.

The first method of calling System.Console.WriteLine uses the fully qualified name, which is very similar to a telephone number consisting of country code plus area code. The compiler knows exactly where to find the Console. The second method, using System, tells the compiler to look in a namespace for the object.

You may have also noticed that Visual Studio Editor has put a wavy line under the word Console. This is its way of telling you where the problem lies. It is a very similar wavy line to the spell checker in Word. If you click on Console when it is underlined, you should see a yellow line appear under the last letter. If you now place your mouse over this yellow box an icon will appear with a drop down arrow next to it. Click on the arrow and Visual Studio will prompt you for different ways of fixing the problem.


In this example it is asking us to select "using System" or "System.Console". If you select one of these actions, the editor will change the code accordingly. Hit Ctrl+Shift+B again and see how it now successfully compiles.

In later tutorials we will use other namespaces; a few are listed below as an example.

System.Data.SqlClient - Database Connection Objects
System.IO - Input / Output Objects
System.Xml.Schema - XML Reader and Writer Objects

Implementing a Namespace

As I said previously, it is good practice to implement namespaces in your projects, so let's have a look at how to implement a namespace in our hello world application.

  1. using System;
  3. namespace MyHelloWorld
  4. {
  5. class HelloWorld
  6. {
  7. static void Main()
  8. {
  9. Console.WriteLine("Hello World!");
  10. }
  11. }
  12. }

It's that easy! Just surround your classes with a namespace block and you're done.

In fact it can be even easier with a nice feature of the IDE. Simply select your classes you wish to include in the namespace right click the code with the mouse and select "Surround With..." and type namespace. The IDE will automatically add in the namespace construct, braces and indent the code for you!

Avoid Conflicts

Namespaces are primarily used for avoiding code conflicts. You could have two developers working on different parts of the same project. They could potentially use the same class name for something inadvertently, and namespaces would be able to resolve the correct class.

Alternatively, you could be using third party software from two companies, and they could use the same class names. Each different class with the same name would perform a slightly different function.


This very simple example illustrates the use of two classes of the same name, which each have a function of the same name. By referencing the namespace you can access the other class's methods directly and unambiguously.

  1. using System;
  3. namespace myInhouseCode
  4. {
  5. class myClass
  6. {
  7. static void Main()
  8. {
  9. Console.WriteLine("myInhouseCode Main");
  10. myMethod();
  11. myContractorsCode.myClass.myMethod();
  12. Console.ReadLine();
  13. }
  15. static void myMethod()
  16. {
  17. Console.WriteLine("myMethod Called From myInhouseCode");
  18. }
  19. }
  20. }
  22. namespace myContractorsCode
  23. {
  24. class myClass
  25. {
  26. public static void myMethod()
  27. {
  28. Console.WriteLine("myMethod Called from myContractors Code");
  29. }
  30. }
  31. }

The output of this code is as follows:

Multiple Namespaces
Multiple Namespaces

In this example we can see that although we have two classes of the same name, and two methods of the same name, by using namespaces we can avoid conflicts, as namespaces allow us to explicitly find the function we require.

Another example of Using

In this final example we can see that our contractor has created a method in a class in a namespace. We are going to call it from our Main method.

  1. using System;
  3. namespace myInhouseCode
  4. {
  5. class myClass
  6. {
  7. static void Main()
  8. {
  9. myContractorsCode.myContractorsClass.myContractorsMethod();
  10. Console.ReadLine();
  11. }
  12. }
  13. }
  15. namespace myContractorsCode
  16. {
  17. class myContractorsClass
  18. {
  19. public static void myContractorsMethod()
  20. {
  21. Console.WriteLine("myContractorsMethod");
  22. }
  23. }
  24. }

myContractorsCode.myContractorsClass.myContractorsMethod(); is a bit long winded, even with IntelliSense, so we can shorten this a little by adding using myContractorsCode; to the using directives and remove the myContractorsCode from the calling statement. This makes our code a little easier to read and type.

Full Example Listing

  1. using System;
  2. using myContractorsCode;
  4. namespace myInhouseCode
  5. {
  6. class myClass
  7. {
  8. static void Main()
  9. {
  10. myContractorsClass.myContractorsMethod();
  11. Console.ReadLine();
  12. }
  13. }
  14. }
  16. namespace myContractorsCode
  17. {
  18. class myContractorsClass
  19. {
  20. public static void myContractorsMethod()
  21. {
  22. Console.WriteLine("myContractorsMethod");
  23. }
  24. }
  25. }

Summary and Conclusions

We have seen namespaces and we now know how they can be used to group together classes in a file. We have also seen how we can use the using directive to tell the compiler where to find code and to make our lives a little easier as well.

Last updated on: Saturday 24th June 2017

Did you Like this Post? Why not Like us on Facebook?


  1. Krishna

    Suprb Tutorial!!!

    Thanks a lot.


  2. Akshay

    Thanks a lot for the tutorial!

Leave a Reply

Your email address will not be published.