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();
}
}
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.
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).
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.
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.
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
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With