Global and local variable scopes

walden systems, geeks corner, programming, languages, C sharp, scope, local scope, global scope, functions, variables, developer, object oriented language, OOP
C# is an elegant and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run on the .NET Framework. You can use C# to create Windows client applications, XML Web services, distributed components, client-server applications, database applications, and much, much more. Visual C# provides an advanced code editor, convenient user interface designers, integrated debugger, and many other tools to make it easier to develop applications based on the C# language and the .NET Framework.

C# has three types of scopes, class-level, method-level and nested level.We will go through each of these different scope in detail but first, let us define what a scope of a variable is. The scope of a variable determines its visibility to the rest of a program. In some programming languages such as Bash, all variables are global unless it is preceded by the "local" keyword. In C#, a variable declared within a loop or other code structure is only visible to the code within the structure.

C# is a strongly typed language which means a variable must be defined with what type of data the variable will hold and can only hold that type of data for the life of the variable. In other programming languages such as Bash, the type of data doesn't have to be defined and can hold any valid data types. One of the implications of this is that in C#, a variable can't be assigned a value or accessed without being defined first. Another implication is that we must be careful assigning values to the variable to make sure that that the type matches.

Both code examples below will result in an error. The first example illustrates attempting to use a variable before it is assigned. The second example illustrates attempting to assign a value with a different data type.

Example 1

    myVar = "foo" ;
    string myVar ;




Example 2
    string  myVar ;
    int       myVar2 ;

    myVar2 = 4 ;
    myVar = myVar2 ;

Class scope

Variables that are defined at the class level are available to any non-static method within the class. In C#, we must use the public keyword to make a class variable accessible from member instances. In Python, we would use the "self." prefix to make a member instance variable accessible from the outside. C# also has class wide variables that are shared by all member instances and can be declared using the static keyword. In Python, any variable declared without the "self." prefix are shared across all instances.

1    class myClass1
2    {
3        public static int myClassInstance = 0;
4    }
5    
6
7    class myClass2
6    {
7        public int myMemberInstance = 5;
8    }

Method scope

Variables declared within a method's code block are available for use by any other part of the method, including nested code blocks. The variables are not available outside of the method. To do this in Bash, we must precede the variable declaration with the "local" keyword.

1    static void Main(string[] args)
2    {
3        int score;                                             
4        score = 100; 
5        if (score >= 95)
6            Console.WriteLine("You got an A : {0}", score);       
7        else
8            Console.WriteLine("Try again : {0}", score);   
9    }


The variable "score" is not available outside the method "Main."

Nested Scope

Variables declared within a nested scope are not available to those outside of their code block. A loop can have multiple levels of nested code blocks and, therefore, multiple levels of nested scope. When a variable is declared within one of these nested scopes, it is visible only to the current scope and any scopes nested within it. This means that a variable declared within a loop is not visible outside of that loop whereas a variable declared outside of the loop is also visible within the loop. This is different from JavaScript which doesn't provide nested scopes at all, making any variable declared inside the code block available throughout. Below is an example of nested scope :

1    int score = 100;

2    if (score >= 95)
3        string message = "You have an A";                     
4    else
5        string message = "Try again";                  
6    Console.WriteLine(message);  


This is an error since the variable "message" is outside the if-then-else code block