C# Namespace3 min read

Namespace in C# is used in a program to organize the code separately from each other. Classes are maintained with namespace to organize in a better way.

With the use of namespace, we can keep one set of names different from another set of names. The vital importance of namespace is that it prevents the collision of the same class names declared in two different namespaces.

A namespace can have Namespaces (Nested Namespace), Classes, Interfaces, Structures, and Delegates.

Defining a Namespace

The keyword “namespace” is used to define the namespace and the namespace can be nested.
Syntax:

Sample example of the namespace:


Accessing members of namespace in C#

The members of the namespace can be accessed using dot(.) operator. The syntax for accessing namespace members is as followed.

namespace_name.member_name

Remember that, two classes inside the same namespace cannot have the same name but the class from a different namespace may have the same class name.


Example 1: C# program to demostrate Namespace

Output:

This is First Namespace
This is Second Namespace

As you can see in the above program, the name of the classes are the same inside the two different namespaces and still, the program did not get affected. We created two different objects for two different namespaces classes and accessed the functions respectively.


The using Keyword

A namespace can be included in the program with the help of using keyword. We already use a name using keyword for System in a program.

Using System in a program we are able to write Console.WriteLine(Hello Simple2code.com") instead of writing the whole System.Console.WriteLine(Hello Simple2code.com"). We include it in the following in the program.

using namespace_name;

Let us go through the above program again and rewrite it using the using keyword.

Output:

This is First Namespace
This is Second Namespace


Nested Namespaces in C#

We can also nest the namespace in a program as already told above. It means inserting a namespace inside a namespace in a program and its members can also be accessed with the use of dot (.) operator.

Syntax of the nested namespace:

Example: C# program for the nested namespace

//Output
Welcome to simple2code.com

If you don’t want to write the whole statement of the main class to create an object then you can simply use using keyword to include the namespace in the following way.

Write the following at the beginning of the program.

And do the following inside the main function of the program.

The output will be the same but you don’t have o repeat the long code again and again in the program.


MORE

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …