.NET C# Global Collision Operator

--

global:: the “global” namespace — It forces the compiler to look for a global type name without considering any other using directives(namespace).

Example:

using static System.Console;


class SampleClass
{
public void ShowMessage()
{
WriteLine($"Global scope class: {nameof(SampleClass)}");
}
}

namespace ConsoleApplication1
{
public class SampleClass
{
public void ShowMessage()
{
WriteLine($"Namespace scope class: {nameof(SampleClass)}");
}
}

class Program
{
static void Main(string[] args)
{
global::SampleClass sampleClassGlobal = new global::SampleClass();
sampleClassGlobal.ShowMessage();

SampleClass sampleClass = new SampleClass();
sampleClass.ShowMessage();
}
}

Output

In essence, this is a method to prevent naming conflicts. You’re likely to encounter it in code generated by tools, especially when the tool lacks awareness of all the other types within the system.

--

--

No responses yet