DEV Community

Haris Iftikhar
Haris Iftikhar

Posted on

Namespaces in C#

Namespaces:
A namespace is designed for providing a way to keep one set of
names separate from another.
The class names declared in one namespace does not conflict with
the same class names declared in another.

Defining a Namespace
A namespaces definition begins with the keyword namespace followed by the namespace name as followed:

namespace namespace_name {
  // your code
}

Enter fullscreen mode Exit fullscreen mode

To call the namespace-enabled version of either function or variable, prepend the namespace keyword as followed:

namespace_name.item_name;
Enter fullscreen mode Exit fullscreen mode

Programming Example:
Programming example

Output
Program output

Nested Namespaces:
You can define one namespace inside another namespace as followed:

namespace namespace_name1 {
  // your code
  namespace namespace_name2 {
    // your code
  }
}
Enter fullscreen mode Exit fullscreen mode

Note: You can access members of nested namespace by using the dot (.) operator.

Programming Example:
Programming example

Output
Program output

Another Way
Programming Example

Output
Image description

Top comments (0)