extern alias and dotted names

  • Thread starter Thread starter Jon Shemitz
  • Start date Start date
J

Jon Shemitz

I'm playing with 2.0's extern alias declarations and the :: operator.
If I have an

extern alias My;

and the My namespace contains a TypeName, I can refer to My::TypeName
.... or to My.TypeName.

I don't quite get this latter usage: I can *not* refer to
global::My.TypeName nor can I refer to global.System.String.

Is My.TypeName an inconsistency, or did I just miss the msdn page that
explains it?
 
Jon Shemitz said:
I'm playing with 2.0's extern alias declarations and the :: operator.
If I have an

extern alias My;

and the My namespace contains a TypeName, I can refer to My::TypeName
... or to My.TypeName.

I don't quite get this latter usage: I can *not* refer to
global::My.TypeName nor can I refer to global.System.String.

Is My.TypeName an inconsistency, or did I just miss the msdn page that
explains it?

I'm not seeing the problem you are in a test program with other names.
Here's my code:

Lib.cs:
using System;

namespace Foo.Bar
{
public class Baz
{
public static void SayHi()
{
Console.WriteLine ("Hello alias");
}
}
}

Test.cs:
extern alias Lib;
using System;

namespace Foo.Bar
{
public class Baz
{
public static void SayHi()
{
Console.WriteLine ("Hello");
}
}
}

class Test
{
static void Main()
{
// Warning (unused variable), but don't worry about it
global::System.String foo = "hello";

Foo.Bar.Baz.SayHi();
global::Foo.Bar.Baz.SayHi();
Lib::Foo.Bar.Baz.SayHi();
}
}

Compile:
csc /target:library Lib.cs
csc Test.cs /r:Lib=lib.dll

Output:
Hello
Hello
Hello alias


It still works if I change "Foo" to "My" everywhere.
 
Jon Skeet said:
I'm not seeing the problem you are in a test program with other names.
Here's my code:

[Most of sample snipped]
global::System.String foo = "hello";

// But while you can NOT say
// global.System.String foo = "hello";
Lib::Foo.Bar.Baz.SayHi();

// You CAN say
// Lib.Foo.Bar.Baz.SayHi();
// but not
// global::Lib.Foo.Bar.Baz.SayHi();


What I really don't get is why I can say

Lib.Foo.Bar.Baz.SayHi();

as if the Lib alias were part of the global namespace - yet I can't
say

// global::Lib.Foo.Bar.Baz.SayHi();
// it's NOT part of the global namespace

or

// global.Lib.Foo.Bar.Baz.SayHi();
// Why "Lib.Foo.Bar.Baz.SayHi();" but not "global.System.string"?
 
Jon Shemitz said:
Jon Skeet said:
I'm not seeing the problem you are in a test program with other names.
Here's my code:

[Most of sample snipped]
global::System.String foo = "hello";

// But while you can NOT say
// global.System.String foo = "hello";

No - because global isn't a namespace, it's an alias.
// You CAN say
// Lib.Foo.Bar.Baz.SayHi();

Eek - that surprises me. That surprises me a *lot*. I suspect it's a
bug in the compiler, but I'll have to check the specs.

<snip>
 
Jon Skeet said:
Eek - that surprises me. That surprises me a *lot*. I suspect it's a
bug in the compiler, but I'll have to check the specs.

That'd be cool. What would you check? Aliases and the :: operator are
not in my printed copy of "The C# Programing Language" nor are they in
the online Word doc.

Would the [Lovely. Clear. Not!] ECMA spec be the place to go?
 
Jon Shemitz said:
Jon Skeet said:
Eek - that surprises me. That surprises me a *lot*. I suspect it's a
bug in the compiler, but I'll have to check the specs.

That'd be cool. What would you check? Aliases and the :: operator are
not in my printed copy of "The C# Programing Language" nor are they in
the online Word doc.

Would the [Lovely. Clear. Not!] ECMA spec be the place to go?

Yup, the ECMA spec it is. It's still in draft form. It's not *hugely*
easy to read, but it's not bad when you get into it.
 
Back
Top