Re: The :: operator, and "global"

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Javaman59 wrote:

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 -- omnibus ex nihilo ducendis sufficit unum Spaces inserted to prevent google email destruction: MSN oliver @ sturmnet.org Jabber sturm @ amessage.de ICQ 27142619 http://www.sturmnet.org/blog .



Relevant Pages