C# Programming/Keywords/global
The global
keyword is useful in some contexts to resolve ambiguity between identifiers. If you have a conflict between a class name and a namespace, for example, you can use the global
keyword to access the namespace:
namespace MyApp
{
public static class System
{
public static void Main()
{
global::System.Console.WriteLine("Hello, World!");
// if we had just used System.Console.WriteLine,
// the compile would think that we referred to a
// class named "Console" inside our "System" class.
}
}
}
global
does not work in the following situation, however, as our System
class does not have a namespace:
public static class System
{
public static void Main()
{
global::System.Console.WriteLine("Hello, World!");
// "System" doesn't have a namespace, so the above
// would be referring to this class!
}
}
C# Keywords | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Special C# Identifiers (Contextual Keywords) | |||||||||||||||
| |||||||||||||||
Contextual Keywords (Used in Queries) | |||||||||||||||
|