What is the difference between “global::System” and “System” in .NET?
-
03-07-2019 - |
Question
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?
Solution
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
OTHER TIPS
To prevent people from doing #1 on this list. :)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow