Docs: global not work

Created on 3 Jun 2019  ·  4Comments  ·  Source: dotnet/docs

Why in the following code error appears?!

using global::System;

namespace System
{
    class Program
    {
        static void Main(string[] args)
        {
            global::System.Console.WriteLine("Hello World!"); // compiler gives warning conflic for Console and error for WriteLine undefined
        }
    }

    class Console
    {
        public static void T()
        {

        }
    }

    class Test1 : global::System.Console // warning conflict
    {
        public static void T1()
        {

        }
    }
}

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Area - C# Guide

Most helpful comment

This is explained in the spec:

Unlike the regular . qualifier, the left-hand identifier of the :: qualifier is looked up only as an extern or using alias.

If you have an alias colAlias, colAlias::Hashtable is guaranteed to use that alias, while colAlias.Hashtable uses the full name lookup rules, which means it could be ambiguous.

You can see the difference in the following code:

```c#
using Foo = Bar;

namespace Foo
{
class C {}
}

namespace Bar
{
class C {}
}

class Test
{
Foo::C c1; // okay
Foo.C c2; // error
}
```

All 4 comments

The global keyword is useful when you want to disambiguate two entities with similar names, e.g. System.Console and TestApp.Console in the example. But you have two types with exactly the same name (System.Console) and global won't help you with that.

If you really want to make this work (and I hope you have a good reason for that, like learning more about C#), you should be able to use the extern alias directive to disambiguate the two assemblies

Than you for the reply. And what is the difference between the followings?

colAlias::Hashtable test = new colAlias::Hashtable();

colAlias.Hashtable test = new colAlias.Hashtable();

This is explained in the spec:

Unlike the regular . qualifier, the left-hand identifier of the :: qualifier is looked up only as an extern or using alias.

If you have an alias colAlias, colAlias::Hashtable is guaranteed to use that alias, while colAlias.Hashtable uses the full name lookup rules, which means it could be ambiguous.

You can see the difference in the following code:

```c#
using Foo = Bar;

namespace Foo
{
class C {}
}

namespace Bar
{
class C {}
}

class Test
{
Foo::C c1; // okay
Foo.C c2; // error
}
```

Hello!

I think you will have a better chance of getting these types of questions answered if you post it on a forum such as StackOverflow. Though I think you did get it answered 😁

I'm going to close the issue.

I am interested though in why you arrived at this article. Are you coming from C++ so the alias::type syntax appealed to you?

Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

skylerberg picture skylerberg  ·  3Comments

LJ9999 picture LJ9999  ·  3Comments

sdmaclea picture sdmaclea  ·  3Comments

gmatv picture gmatv  ·  3Comments

mekomlusa picture mekomlusa  ·  3Comments