C# namespaces provide a way to organize related classes and other types.
A namespace is a logical grouping which means the class with the same namespace can be in different assemblies or components.
using System;
using MyProject.MyGroup.MyNamespaceOne; // using directive
using MNTwo = MyProject.MyGroup.MyNamespaceTwo; // namespace Alias
TestClass testOne = new TestClass();
testOne.TestMethodOne();
MNTwo.TestClass testTwo = new MNTwo.TestClass();
testTwo.TestMethodTwo();
namespace MyProject.MyGroup.MyNamespaceOne
{
public class TestClass
{
public void TestMethodOne()
{
Console.WriteLine("this is from namespace: MyProject.MyGroup.MyNamespaceOne.");
}
}
}
namespace MyProject.MyGroup.MyNamespaceTwo
{
public class TestClass
{
public void TestMethodTwo()
{
Console.WriteLine("this is from namespace: MyProject.MyGroup.MyNamespaceTwo");
}
}
}
Output:
this is from namespace: MyProject.MyGroup.MyNamespaceOne.
this is from namespace: MyProject.MyGroup.MyNamespaceTwo.