VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/namespaces-c-sharp/

⇱ Namespaces in C# - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Namespaces in C#

Last Updated : 4 Sep, 2025

In C#, a namespace is a way to organize and group related classes, interfaces, structs and other types. It helps avoid name conflicts and makes code easier to manage, especially in large projects.

Defining a namespace

To define a namespace in C#, we will use the namespace keyword followed by the name of the namespace and curly braces containing the body of the namespace as follows:

Syntax

namespace name_of_namespace {
// Namespace (Nested Namespaces)
// Classes
// Interfaces
// Structures
// Delegates
}

Example

Accessing the Members of Namespace

The members of a namespace are accessed by using dot(.) operator. A class in C# is fully known by its respective namespace.

Syntax

[namespace_name].[member_name]

Note:

  • Two classes with the same name can be created inside 2 different namespaces in a single program.
  • Inside a namespace, no two classes can have the same name.
  • In C#, the full name of the class starts from its namespace name followed by dot(.) operator and the class name, which is termed as the fully qualified name of the class.

Example:


Output
Hello Geeks!

In the above example:

  • In System.Console.WriteLine()" "System" is a namespace in which we have a class named "Console" whose method is "WriteLine()".
  • It is not necessary to keep each class in C# within Namespace but we do it to organize our code well.
  • Here "." is the delimiter used to separate the class name from the namespace and function name from the classname.

The using keyword

In C#, calling a class or function with its fully qualified name (namespace + class) every time is not practical.

For example:

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

first.Geeks_1.display();

To avoid this, C# provides the using keyword. By adding using at the top of the program, you can access the classes and methods of that namespace directly, without writing the full name each time.

Syntax

using [namespace_name][.][sub-namespace_name];

In the above syntax, dot(.) is used to include subnamespace names in the program.

Program to illustrate the use of using keyword

Output:

Hello Geeks!

Nested Namespaces

You can also define a namespace into another namespace which is termed as the nested namespace. To access the members of nested namespace user has to use the dot(.) operator.

For example: Generic is the nested namespace in the collections namespace as System.Collections.Generic

Syntax

namespace name_of_namespace_1
{
// Member declarations & definitions
namespace name_of_namespace_2
{
// Member declarations & definitions
.
.
}
}

Program:


Output
Nested Namespace Constructor
Comment
Article Tags:
Article Tags:

Explore