What Is the Global Variable in C# in Ubuntu 20.04?
The Global variables are accessible from any place inside a class or namespace. Although C# does not allow global variables directly, the functionality of global variables can be accomplished by creating a static class, which is useful in some scenarios. In C#, a public static variable is used to declare inside a public class and used as a global variable. Its global scope ensures that it retains its usefulness throughout the program’s lifespan. As a result, it can be utilized by any function declared within the program unless shadowed.
Important Features of the Global Variable in C# in Ubuntu 20.04:
- The global variable can be used from all functions or modules of the class in a program.
- It is formed before the start of the program’s global execution and is discarded when it ends.
- Data sharing is feasible because different functions can utilize the same global variable.
- A global variable does not require parameter passing because it is available throughout the program.
- When the global variable’s value is changed in one function, the whole of the program changes as well.
- Any function or module in the program has access to the global variables.
- It is stored in fixed memory storage predetermined by the compiler.
How to Use the Global Variable in C# in Ubuntu 20.04:
We have various ways to declare the global variable in the C# programming language, although C# itself does not support the global variables.
Example # 1: Difference Between the Local Variable and Global Variable in C# in Ubuntu 20.04:
Local variables are declared in the function and are defined using statements within the function. On the other hand, the global variables are declared outside the function. The following example program declares both the local and global variables to clear the difference between these two variables:
The program starts with importing the system library “using System”, which will allow us to access the function and module of C# where required. The namespace is given the name “global_variable”. Then, we have a class with the public static keywords, and the class is given the name “Global”. The Global variable is declared in the class “Global” as “Difference” of type integer property. Another class is created as “Program1”, in which the local variables are defined as “a” and “b” of type integer. Only the specified class function makes use of these local variables. They are restricted to defining outside the function of the specified class.
The variable “sum” is created, which has the variables “a” and “b” with the add operator to get the sum of these two variables. The global variable is invoked here using its class name “Global”. The global variable gets the difference from the local variables by using the difference operator. The writeLine method will print the values from the local variable “sum” and the global variable “Difference”.
The following image shows the values from the local variable declaration and global variable declaration inside the C# program:
Example # 2: Declaring the Global Variable Inside the Class in C# in Ubuntu 20.04:
In the C # program, we have to use a public static variable declared inside a public class as a global variable in the C# program. The following code displays declaring a global variable in C# with the help of a public class:
The C# program declares the namespace “GlobalVariable.”. We have the class “Global” defined with the keyword public and static in the namespace. Inside the class, we have created variables “MyName” and “MyAge” used globally. These variables are declared using the keywords “public” and “static”. The static keyword implies that we don’t need to build a class object to get the variables, and the public keyword specifies that these variables can be accessed outside the class. Then, another class, “Program1”, is created in the namespace. In that class, we have initialized the values of these global variables by calling it with the global class. The writeLine method will print the values of the global variables.
The output of the program, as mentioned above, is as follows:
Example # 3: Declaring the Global Variable With Public Static Property Inside the Class in C# in Ubuntu 20.04:
We can also use the static property instead of declaring a public variable in a class if we don’t want to define a public variable.
The public class “Global3” is defined in the namespace “GlobalVariableExample”. In the class, we have declared the public static property “Name” and “Age”. These properties are assigned the get method and set method. The get function returns the value of these properties, whereas the set method allows us to assign values to them. Then, in the class “Program”, we are accessing the properties “Name” and “Age” as Global.Name and Global.Age. Note that we can declare the Global class outside the “GlobalVariableExample” namespace if we want to access the Global. Name and Global.Age field.
The output is shown on the console screen as follows:
Example # 4: Using the Global Namespace Alias With Scope Resolution Operator in C# in Ubuntu 20.04:
Global variables are not directly supported in C#, and the scope resolution operator for global variables in C# is tied to namespaces. It is called a global namespace alias. We are going to use the global namespace alias in the following program:
The C# program has a class “Demo”, which we have called the main function. The main function block has a scope resolution used with a global namespace alias declared with the keyword “using” at the beginning of the program. When using the scope resolution operator with the System.console class, utilize the global namespace alias as a reference. The Hashtable is a System.console class called here to get the key/value pairs of the items defined in the “hTable” variable. The items are included by the Add() method. With the foreach loop, we can access the key/value pairs of the items in the “hTable”, which is assigned to “myStr” in the foreach loop.
The output of the previous program is shown on the following terminal screen:
Conclusion:
The article aims to show you the ways to use variables globally. We clearly define the global variable and discuss several important features of using the global variable. Then, to demonstrate the notion and distinction between local and global variables, we have examples of local variables and global variables. This article also covers the many methods for accessing the global variable. You might get a good understanding of how to use a global variable in C#.