Why is the double colon(::) operator required to resolve a namespace conflict?

13,389

Solution 1

It is mainly needed when someone wrote code without consideration of code being used. I.e. duplicate classes in namespaces that are expected to be used together or hiding namespaces.

MSDN sample shows one case in Use the Global Namespace Alias :

class TestApp
{
    // Define a new class called 'System' to cause problems. 
    public class System { }

    // Define a constant called 'Console' to cause more problems. 
    const int Console = 7;
    const int number = 66;

    static void Main()
    {
        // The following line causes an error. It accesses TestApp.Console, 
        // which is a constant. 
        //Console.WriteLine(number);

        global::System.Console.WriteLine(number); // ok

    }
}

Solution 2

the :: operator doing the same like namespace. ,but the :: operator is used to look up identifiers. It is always positioned between two identifiers

example :

global::System.Console.WriteLine("Hello World");

a good example explained here : http://msdn.microsoft.com/en-us/library/c3ay4x3d.aspx

Share:
13,389

Related videos on Youtube

Deepak Raj
Author by

Deepak Raj

1. 5+ years of experience as a .NET Developer/ Lead. 2. 4 years and 2 months of working experience in the E&U domain for an web application development. 3. Proficient in Web Application Development on .Net Environment with good knowledge in SQL Server 2005/2010. 4. Possess sound technical knowledge of C#.NET, ASP.NET, JavaScript, XML, Web Services, Design Patterns, OO Principles,Single Sign On, Unit Testing Projects in VS 2010, Excel Macro (VBA). 5. Have been involved in SDLC projects from start to end. 6. Experienced in the usage of tools like Team Foundation Server, eprise CMS, IIS Server and TIBCO Administrator.

Updated on September 12, 2022

Comments

  • Deepak Raj
    Deepak Raj over 1 year

    Please see my below sample program. I have two namespaces containing the same struct. To avoid conflict while using in Main(), I have given the namespaces aliases. While invoking the struct from Main(), I am able to invoke directly through namespace alias, like test.MyStruct. I have another option also using :: operator, like test::MyStruct.

    Why is the :: operator required, and where should I use it instead of an alias?

    using System;
    using test=counter;
    using duplicatecounter;
    
    namespace counter
    {
        struct MyStruct
        {
    
        }
    }
    
    namespace duplicatecounter
    {
        struct MyStruct
        {
    
        }
    }
    
    class Program
    {
        public static void Main()
        {
            test.MyStruct a = new test.MyStruct();
            test::MyStruct a1 = new test::MyStruct();
        }
    }
    
  • Deepak Raj
    Deepak Raj about 11 years
    great. now I understand global:: form does not have the alias version like global.. Good answer Eslam. Anyway, what does identifiers mean in your example? I am confused with the reasoning.
  • Deepak Raj
    Deepak Raj about 11 years
    well, I was able to do this using global = System; class TestApp { static void Main() { global.Console.WriteLine("test"); } } but fine, i got the answer..
  • Deepak Raj
    Deepak Raj about 11 years
    thanks Alexei for your detailed explanation.
  • Maverick Meerkat
    Maverick Meerkat almost 6 years
    It is actually not needed for any aliased name space except the global. If you use any other alias, you can use the regular c# dot syntax (i.e. SomeAliasedAssembly.Namespace.Class.etc...)