The :: operator, and "global"

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I saw in a recent post the :: operator used to reach the global namespace, as
in

global::MyNamespace

I hadn't seen this before, so looked it up in MSDN, which explained it
nicely. My question is, do "global" and "::" always go together? Is there any
other use for these operators, than as a pair?

TIA,

Javaman
 
Javaman,

AFAIK, there's no "global" keyword in c# 2.0. You only use the operator '::'
to indicate the compiler to start the search from the 'outest' scope:

namespace A {
namespace System {
class X {
public void Message(string s)
{
::System.Windows.Forms.MessageBox.Show(s); // '::' needed here
}
}
}
}

Regards - Octavio
 
You should probably check before you post. Your code will not compile - thats a C++ construct. There is a global keyword in C# v2 and it always goes together with :: to indicate global scope of the type.

global::System.Windows.Forms.MessageBox.Show("foo");

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Javaman,

AFAIK, there's no "global" keyword in c# 2.0. You only use the operator '::'
to indicate the compiler to start the search from the 'outest' scope:

namespace A {
namespace System {
class X {
public void Message(string s)
{
::System.Windows.Forms.MessageBox.Show(s); // '::' needed here
}
}
}
}

Regards - Octavio


Javaman59 said:
I saw in a recent post the :: operator used to reach the global namespace,
as
in

global::MyNamespace

I hadn't seen this before, so looked it up in MSDN, which explained it
nicely. My question is, do "global" and "::" always go together? Is there
any
other use for these operators, than as a pair?

TIA,

Javaman



[microsoft.public.dotnet.languages.csharp]
 
Richard,

Thanks for your correction.
Actually I did that check, referring to a MSDN Magazine I always considered
excellent:

http://msdn.microsoft.com/msdnmag/issues/04/05/c20/default.aspx

Maybe the article is little bit old and specs have changed since then?

Regards - Octavio

Richard Blewett said:
You should probably check before you post. Your code will not compile -
thats a C++ construct. There is a global keyword in C# v2 and it always
goes together with :: to indicate global scope of the type.

global::System.Windows.Forms.MessageBox.Show("foo");

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Javaman,

AFAIK, there's no "global" keyword in c# 2.0. You only use the operator
'::'
to indicate the compiler to start the search from the 'outest' scope:

namespace A {
namespace System {
class X {
public void Message(string s)
{
::System.Windows.Forms.MessageBox.Show(s); // '::' needed here
}
}
}
}

Regards - Octavio


Javaman59 said:
I saw in a recent post the :: operator used to reach the global
namespace,
as
in

global::MyNamespace

I hadn't seen this before, so looked it up in MSDN, which explained it
nicely. My question is, do "global" and "::" always go together? Is
there
any
other use for these operators, than as a pair?

TIA,

Javaman



[microsoft.public.dotnet.languages.csharp]
 
Javaman59 said:
I saw in a recent post the :: operator used to reach the global namespace, as
in

global::MyNamespace

I hadn't seen this before, so looked it up in MSDN, which explained it
nicely. My question is, do "global" and "::" always go together? Is there any
other use for these operators, than as a pair?

Apart from the other replies, there's another, maybe more common, use
for the :: operator. You know the way you can define aliases in a using
statement? Like this:

using SWF = System.Windows.Forms;

Now you can use SWF as a shortcut for the System.Windows.Forms
namespace. In .NET 1.1, you used to write

SWF.Form

or whatever to refer to a type within the aliased namespace. This could
obviously be an ambiguous expression in certain circumstances, so in
..NET 2, the :: operator was introduced to be used in this situation. So
in .NET 2 you'd write

SWF::Form

instead, which is no longer ambiguous. The "global" alias is nothing
more than a default alias for the topmost namespace and can't be used in
any other context, as the other posters already said.


Oliver Sturm
 
Thanks Octavio for answering my question. I had a quick look at the article
too, and it does say the "::" is valid as a prefix (ie. your code should
compile). Maybe someone was hoping that c++ style "::" would return, but
Microsoft eventually decided against it :). I'm glad, too, because I always
found that dangling "::" confusing - "global::" is much cleareer!

- Javaman

Octavio Hernandez said:
Richard,

Thanks for your correction.
Actually I did that check, referring to a MSDN Magazine I always considered
excellent:

http://msdn.microsoft.com/msdnmag/issues/04/05/c20/default.aspx

Maybe the article is little bit old and specs have changed since then?

Regards - Octavio

Richard Blewett said:
You should probably check before you post. Your code will not compile -
thats a C++ construct. There is a global keyword in C# v2 and it always
goes together with :: to indicate global scope of the type.

global::System.Windows.Forms.MessageBox.Show("foo");

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Javaman,

AFAIK, there's no "global" keyword in c# 2.0. You only use the operator
'::'
to indicate the compiler to start the search from the 'outest' scope:

namespace A {
namespace System {
class X {
public void Message(string s)
{
::System.Windows.Forms.MessageBox.Show(s); // '::' needed here
}
}
}
}

Regards - Octavio


Javaman59 said:
I saw in a recent post the :: operator used to reach the global
namespace,
as
in

global::MyNamespace

I hadn't seen this before, so looked it up in MSDN, which explained it
nicely. My question is, do "global" and "::" always go together? Is
there
any
other use for these operators, than as a pair?

TIA,

Javaman



[microsoft.public.dotnet.languages.csharp]
 
Thanks Oliver,

That's really what I was looking for. MSDN on the "::" operator says that it
is placed between two identifiers, but it isn't clear how it can be used,
apart from "global::SomeNamespace". Your reply clears it up for me.

- Javaman
 
One final (minor) point....
MSDN on the "::" operator says that it
is placed between two identifiers

Note that is says "identifier" and not "keyword".

Technically, global is not a keyword (neither are get, set, value, partial,
alias, add, remove, where, yield).
 
Javaman,

Researching on this topic I've found that there is already an official
standard for C# 2.0:

http://www.ecma-international.org/publications/standards/Ecma-334.htm

Of course it fully describes the usage of the '::' operator and the 'global'
identifier.

Thanks and best regards - Octavio

Javaman59 said:
Thanks Octavio for answering my question. I had a quick look at the
article
too, and it does say the "::" is valid as a prefix (ie. your code should
compile). Maybe someone was hoping that c++ style "::" would return, but
Microsoft eventually decided against it :). I'm glad, too, because I
always
found that dangling "::" confusing - "global::" is much cleareer!

- Javaman

Octavio Hernandez said:
Richard,

Thanks for your correction.
Actually I did that check, referring to a MSDN Magazine I always
considered
excellent:

http://msdn.microsoft.com/msdnmag/issues/04/05/c20/default.aspx

Maybe the article is little bit old and specs have changed since then?

Regards - Octavio

"Richard Blewett [DevelopMentor]" <[email protected]> escribió
en
el mensaje news:%[email protected]...
You should probably check before you post. Your code will not compile -
thats a C++ construct. There is a global keyword in C# v2 and it always
goes together with :: to indicate global scope of the type.

global::System.Windows.Forms.MessageBox.Show("foo");

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Javaman,

AFAIK, there's no "global" keyword in c# 2.0. You only use the operator
'::'
to indicate the compiler to start the search from the 'outest' scope:

namespace A {
namespace System {
class X {
public void Message(string s)
{
::System.Windows.Forms.MessageBox.Show(s); // '::' needed here
}
}
}
}

Regards - Octavio


"Javaman59" <[email protected]> escribi? en el
mensaje
I saw in a recent post the :: operator used to reach the global
namespace,
as
in

global::MyNamespace

I hadn't seen this before, so looked it up in MSDN, which explained
it
nicely. My question is, do "global" and "::" always go together? Is
there
any
other use for these operators, than as a pair?

TIA,

Javaman



[microsoft.public.dotnet.languages.csharp]
 
Thanks Mark. That is actually very helpful, and worth remembering. Now I know
why I couldn't find "global keyword" in the help index.

- Javaman
 
Back
Top