In this article, we will find out about Namespaces in C#. What is Namespace, how to utilize Namespace in the program? Learn with Examples.
C# Namespace
In C# namespaces are utilized to bunch comparable kind of classes. Two classes with the same name in various namespaces never struggle to one another.
In C# namespace can be:
- Client characterized
- Pre characterized, that is an in-inherent .NET class library
Here, we have to utilize utilizing watchword to get to characterized namespaces.
Syntax:
namespace <namespace_name>
{
//Write code here
}
Note:
- To announce client characterized namespace we have to utilize namespace catchphrase.
- In the event that we need to get to a class characterized inside a namespace, at that point we need use. (spot) Operator.
Example:
using System;
using System.Collections;
namespace namespace1
{
class ABC
{
public void fun()
{
Console.WriteLine("Inside Namespace1");
}
}
}
namespace namespace2
{
class ABC
{
public void fun()
{
Console.WriteLine("Inside Namespace2");
}
}
}
class Program
{
static void Main()
{
namespace1.ABC OB1 = new namespace1.ABC();
namespace2.ABC OB2 = new namespace2.ABC();
OB1.fun();
OB2.fun();
}
}
Output
Inside Namespace1
Inside Namespace2