Global
KeywordNamespaces are versatile and enable the creation of complex infrastructures of objects. There could be situations in which you need to define namespaces with names already defined in the .NET Framework, such as System
. Although this should never be done, you might face some problems. Consider the following code:
Such code will not be compiled because the compiler looks for a NotImplementedException
within our System
namespace and not the one of the Base Class Library. Although such a scenario is unusual, Microsoft’s developers provided a way for avoiding such problems. You can use the Global
keyword to ensure that the compiler invokes the specified member from the Base Class Library namespaces and not from custom ones. In our case, the Throw
statement should be rewritten as follows:
Throw New Global.System.NotImplementedException
With this approach we can be sure that the appropriate objects from the Base Class Libraries namespaces will be invoked. This approach is always used behind the scenes by Visual Studio for auto-generated code. To get an example, you can simply inspect My Project’s files.
18.221.66.31