Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Defining Namespace
3.
Example:
4.
Accessing class of Namespace
5.
Example:
6.
Using Keyword
7.
Nested Namespace
8.
Example:
9.
FAQs
10.
Key Takeaways
Last Updated: Mar 27, 2024

C# Namespace

Author yuvatimankar
1 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

C# namespace is used to provide a level of code separation. A class name declared in one class does not conflict with the class name in another class. It allows us to break up our code to be easier to understand and maintain, especially when the program is extensive. Also, it can enable us to avoid conflicts with names in our code. 

A namespace is not mandatory for writing the C# program, but it is an essential practice for writing a cleaner code. For example, if all books are stored together in a library, it will be difficult for the reader to find the book related to his need. That is why there are different sections in the library, so that related books are kept close, and it is easy for the reader to find the book. The same goes for the namespace in C# namespace; it helps us to organise different namespaces by putting similar members in a single namespace. In  C# namespace, Two or more classes can have the same name when put into a different namespace.

Recommended Topic, Palindrome in C#, singleton design pattern in c#, and  Ienumerable vs Iqueryable

Defining Namespace

To define a C# namespace, we have to use the keyword namespace followed by namespace-name -

namespace namespace_name {
// code declarations
}
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example:

namespace MyNamespace{
	class Myclass{
	Static void Main(string[] args){
		System.Console.writeline(“Hello World”);
		}
	}
}

In this example, Mynamespace is a namespace-name in which we have created a class named Myclass.Here we used System.Console in which system is the Namespace and Console is the class.

Accessing class of Namespace

For accessing the C# namespace class, we use the (.) operator. The syntax for accessing class of Namespace is as follows -

Namespace-name.class.name

If we consider the above example, if we want to create an object of Myclass, the following syntax will follow -

MyNamespace.Myclass myclass = new MyNamespace.Myclass();

Example:

using System;
namespace MyNamespace
{
    public class SampleClass
    {
        public static void myMethod()
        {
            Console.WriteLine("Namespace created");
        }
    }
}
 namespace MyProgram
{
    public class MyClass
    {
        public static void Main()
        {
            MyNamespace.SampleClass.myMethod();
        }
    }
}

Output: Namespace created

In this Program, MyNamespace is our Namespace which we have created and accessed using the Main() method inside Myclass. In Main() , myMethod() is called using (.) operator.

Using Keyword

Using keyword in C# namespace is used to avoid writing complete names to access the member repeatedly. For example, we use the syntax as using Namespace-name; instead, we can operate using System;

Once we have written 

Using System; 

We can write 

Console.WriteLine(“hello World”);

Instead of 

System.Console.WriteLine(“hello World”);

Nested Namespace

A namespace that can contain another namespace is called a Nested namespace.  We must use the (.) operator to access the Namespace member.

The syntax for creating a nested namespace is as follows:

namespace MyNamespace
{
    namespace NestedNamespace
    {
  		// Body of nested Namespace
     }
}

Example:

using System;

// Nested Namespace
namespace MyNamespace
{
 namespace Nested
    {
        public class SampleClass
        {
            public static void myMethod()
            {
                Console.WriteLine("Nested Namespace ");
            }
        }
    }
}
 
namespace MyProgram
{
    public class MyClass
    {
        public static void Main()
        {
            MyNamespace.Nested.SampleClass.myMethod();
        }
    }
}

Output: Nested Namespace

Here, we have created another namespace named  Nested inside Mynamespace. So, if we want to access a Namespace member, we have to use MyNamespace.Nested.SampleClass.myMethod , instead of  MyNamespace.SampleClass.myMethod().

FAQs

1. Why do we use Namespace in C#?

Ans: C# namespace is used to avoid conflict between names. Also, it is used to organise many classes so that it is easy to handle the application.

2. Can we define one Namespace inside another?

Ans: Yes, we can use one C# namespace into another namespace, known as the Nested Namespace.

3. Is using Namespace in C# necessary?

Ans: No, it is not; however, it is considered a good practice. Using C# namespace makes it easier to maintain our code and avoid name conflicts in a program.

Key Takeaways

In this article, we have seen everything about the C# namespace.  We have discussed Namespace, defining a namespace, using the “using” keyword, and nested Namespace.

If you think this blog helped you, please share it. And to learn further about C#, You can visit Coding Ninjas Studio!

 

Previous article
Interfaces in C#
Next article
Exception Handling in C#
Live masterclass