Namespace Alias Qualifier in C#



namespace and it provides a way to avoid ambiguous definitions of the classes. It is always positioned between two identifiers. The qualifier looks like two colons(::) with an alias name and the class name. It can be global. Thus it doesn’t invoke a lookup in the aliased namespace but in the global namespace.

	using System;
	using NS1;
	using NS2;
	
	using MyNS = NS1;
	using YourNS = NS2;

	namespace NS1
	{
		public class MyClass
		{
			public void Display()
			{
				Console.WriteLine("Display() Method of MyClass in NS1");
			}
		}
	}
	
	namespace NS2
	{
		public class MyClass
		{
			public void Display()
			{
				Console.WriteLine("Display() Method of MyClass in NS2");
			}
		}
	}
	
	class UsingMyClass
	{
		public static void Main()
		{
			MyNS::MyClass myObject = new MyNS::MyClass();
			myObject.Display();
			
			YourNS::MyClass yourObject = new YourNS::MyClass();
			yourObject.Display();
		}
	}

0 Comment's

Comment Form

Submit Comment