Using the Namespace Alias Qualifier in C#

One of the advantages of the Namespace is the organization of your code and structure .

You could use the Namespace Alias Qualifier that allows the developers to replace the namespace names with the alias so that the ambigous definitions of the classes/namespaces are prevented.

An sample example of the Ambigous definition os class is below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Name1
{
  class Employee
  {
     public string Name { get; set; }
  }
}
namespace Name2
{
  class Employee
  {
     public string Name { get; set; }
  }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Name1;
using Name2;
namespace WindowsFormsApplication1
{
static class Program
{
///
 /// The main entry point for the application. /// 

static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
var obj = new Employee(); } }
}

 

In the above example , the class “Employee” is defined in the namespace “Name1” and “Name2” which is referenced in the . You can prevent the Ambigous definition with the help of the Namespace Alias Qualifier(::) .

The below Program.cs includes the Namespace Alias Qualifier which solves this issue

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Name1;
using Name2;
namespace WindowsFormsApplication1
{
using Employee1 = Name1;
using Employee2 = Name2;

static class Program
{
 /// The main entry point for the application. /// 

static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
var obj = new Employee1.Employee(); } } }