.NET Zone is brought to you in partnership with:

Senthil Kumar is a Software Engineer who has around 3 years of experience in IT industry. He is currently working as a Software Engineer in Bangalore and works mainly on the Windows or Client Development technologies and has good working experience in C#/.NET, Delphi, Winforms and SQL Server. He is also a Microsoft Technology Certified Professional in ASP.NET. He Blogs at http://www.ginktage.com and http://www.windowsphonerocks.com. He enjoys learning as much as he can about all things related to technologies to get a well-rounded exposure of technologies that surround him. Senthil completed his Master of Computer Applications from Christ College (Autonomous), Bangalore in the year 2009 and is a MCA Rank Holder. He has passion for Microsoft technologies especially Windows Phone development. You can connect with him on Twitter at (http://twitter.com/isenthil) , on Facebook at (http://www.facebook.com/kumarbsenthil) and his blog (www.ginktage.com). Senthil is a DZone MVB and is not an employee of DZone and has posted 121 posts at DZone. You can read more from them at their website. View Full User Profile

Namespace Alias Qualifier in C#

08.21.2012
| 1821 views |
  • submit to reddit

The Namespace in C# has the following advantages

  • It lets the developers organize their code / classes
  • Provides better readability of the code and helps you understand how the code structure is formed, especially in bigger projects.

The Namespace Alias Qualifier in C# lets developers use the alias name instead of the complete namespace name. The advantage of the Namespace Alias Qualifier is that it lets you use the alias name instead of a bigger namespace ( like Inner Namespaces ) and it also helps to avoid the ambiguous definitions of the classes.

For example

Take the below class as an example

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

namespace Namespace1
{
    class Student
    {
        public string StudentName { get; set; }
    }
}
namespace Namespace2
{
    class Student
    {
        public string StudentName { get; set; }
    }
}

When both Namespace1 and Namespace2 are referenced in the C# file and are trying to create an instance of Student, it will cause the “Ambiguous name” error as shown in the screenshot below.

 

To avoid this error, we could use the :: operator to provide an alias name for the namespace and use them accordingly.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{

    using stud1 = Namespace1;
    using stud2 = Namespace2;
    static class Program
    {
        static void Main()
        {
            var obj = new stud1 :: Student();
        }
    }
}
Published at DZone with permission of Senthil Kumar, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)