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

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

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 …