What is the difference between "global :: System" and "System" in .NET?

I just upgraded the VS 2005 project to VS 2008 and studied the changes. I noticed that one of the .Designer.cs files has changed significantly. Most of the changes were simply replacing System with global :: System. For instance,

protected override System.Data.DataTable CreateInstance()

has become

protected override global::System.Data.DataTable CreateInstance()

What's going on here?

+3
source share
2 answers

The :: this operator is called the alias qualifier for aliases.

 global::System.Data.DataTable 

matches with:

 System.Data.DataTable

Visual Studio 2008 added it to the code generated by the designer to avoid the ambiguous reference problems that sometimes happened when people created classes called System ... For example:

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()
    {
        // Error  Accesses TestApp.Console
        //Console.WriteLine(number);
    }
}

However:

global::System.Console.Writeline("This works");

For further reading:

http://msdn.microsoft.com/en-us/library/c3ay4x3d(VS.80).aspx

+12

# 1 .:)

+1

Source: https://habr.com/ru/post/1697814/


All Articles