views:

292

answers:

2

I just upgraded a VS 2005 project to VS 2008 and was examining the changes. I noticed one of the .Designer.cs files had changed significantly. The majority of the changes were simply replacements of System with global::System. For example,

protected override System.Data.DataTable CreateInstance()

became

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

What's going on here?

+12  A: 

The :: operator is called a Namespace Alias Qualifier.

 global::System.Data.DataTable

is the same as:

 System.Data.DataTable

Visual Studio 2008 added it to the designer generated code to avoid ambigious reference issues that occasionally happened when people created classes named 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

FlySwat
Thanks. I couldn't get any good hits on google because of the colons, I suppose. However, there is no custom System class or namespace in this project, so I don't know why it felt the need to fully qualify those System references.
raven
+1  A: 

To prevent people from doing #1 on this list. :)

TheSoftwareJedi