C# Keywords Tutorial Part 39: global

C# Keywords Tutorial Part 39: global

The C# language is an object-oriented programming language that is widely utilized in conjunction with the .NET framework to build a diverse range of applications, such as desktop and mobile apps, web applications, and games. This blog post delves into the “global” keyword in C#, which eases the process of accessing variables defined in the global namespace. The “global” keyword made its debut in C# 9 and is especially useful when there are multiple variables with the same names but differing namespaces. Using “global” enables developers to avoid the repetitive specification of the namespace when accessing a variable.

Here’s a simple example to demonstrate how the “global” keyword can be used in C#:

namespace MyNamespace1
{
    class MyClass
    {
        public string myString = "Hello from MyNamespace1";
    }
}

namespace MyNamespace2
{
    class MyClass
    {
        public string myString = "Hello from MyNamespace2";
    }
}

class Program
{
    static void Main(string[] args)
    {
        MyNamespace1.MyClass myClass1 = new MyNamespace1.MyClass();
        MyNamespace2.MyClass myClass2 = new MyNamespace2.MyClass();

        Console.WriteLine(global::MyNamespace1.myClass1.myString);
        Console.WriteLine(global::MyNamespace2.myClass2.myString);
    }
}

In this example, we have two classes with the same name (“MyClass”), but they are defined in different namespaces (“MyNamespace1” and “MyNamespace2”). In the “Main” method, we create instances of both classes and then use the “global” keyword to access the “myString” variable of each class.

By using the “global” keyword, we can access the variables without having to specify the namespace every time. This makes our code more concise and easier to read.

Another use case for the “global” keyword is when you want to access a variable that is defined outside of the current namespace. Here’s an example:

namespace MyNamespace1
{
    public class MyClass
    {
        public static string myString = "Hello from MyNamespace1";
    }
}

namespace MyNamespace2
{
    public class MyClass
    {
        public static string myString = "Hello from MyNamespace2";
    }
}

namespace MyNamespace3
{
    public class MyClass
    {
        public void MyMethod()
        {
            Console.WriteLine(global::MyNamespace1.MyClass.myString);
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        MyNamespace3.MyClass myClass3 = new MyNamespace3.MyClass();
        myClass3.MyMethod();
    }
}

In this example, we have three namespaces, each with a “MyClass” class that contains a static string variable called “myString”. In the “MyNamespace3.MyClass” class, we define a method called “MyMethod” that uses the “global” keyword to access the “myString” variable of the “MyNamespace1.MyClass” class.

By using the “global” keyword, we can access the variable from a different namespace without having to fully qualify the name with the namespace. This makes our code more concise and easier to read.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *