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!