Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
    }
}
like image 490
Deepak Raj Avatar asked Mar 04 '13 16:03

Deepak Raj


People also ask

Why does C++ use double colon?

Two colons (::) are used in C++ as a scope resolution operator. This operator gives you more freedom in naming your variables by letting you distinguish between variables with the same name.

What does double colon in python mean?

Answer: The double colon is a special case in Python's extended slicing feature. The extended slicing notation string[start:stop:step] uses three arguments start , stop , and step to carve out a subsequence. It accesses every step -th element between indices start (included) and stop (excluded).

What does double colon mean in logic?

The double colon ( :: ) may refer to: an analogy symbolism operator, in logic and mathematics. a notation for equality of ratios. a scope resolution operator, in computer programming languages.

What does double colon mean in Java?

The double colon (::) operator, also known as method reference operator in Java, is used to call a method by referring to it with the help of its class directly. They behave exactly as the lambda expressions.


2 Answers

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

    }
}
like image 85
Alexei Levenkov Avatar answered Nov 07 '22 03:11

Alexei Levenkov


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

like image 23
Eslam Hamouda Avatar answered Nov 07 '22 04:11

Eslam Hamouda