Loading, please wait...

A to Z Full Forms and Acronyms

Explain the concept of scope of variable in C#

Jun 11, 2020 Scope, variable, 8708 Views
In this article you will learn about the concept of scope of variable in C#

Explain the concept of the scope of variables in C#

The scope of a variable specifies the area where one can use that variable in the program. This means that the scope determines the region within the program where the variable can be accessed and referenced. The region of source code over which the definition of a variable. In other words, the lifetime of a variable and its accessibility is referred to as scope.

Mainly there are three types of variables:

1. Local Variables

Local variables exist within the scope of a given method in a class like the scope of a local variable is within the method where it has been declared.

Example:

Class sample
{

public void methodsum()
{

int x = 10;

int y = 20;

...

...

}

In the above code, x and y are local variables whose scope is within the method method-sum( ). the moment the control comes out of the method in which the variable is defined, the variable and its value is lost.

A local variable can not be defined as public or private because they are not accessible outside the method in which they are defined.

2. Static Variables

Static variables are the variables that retain their value throughout the program. There is only one copy of the static variable for all instances of a class. These variables are accessible from anywhere in the class in which they are defined. The static variables are declared with the static keyword. The life of the static variable loses its existence when the associated application domain loses its existence. One can access the value of static variables through the class name.

Example:

using System;


class program
{
public static int X=200; /* static variable*/
static void main(string[]args)
{

Console.write(“{0}\t”,X); /* 200 */
Program. X=400; /* This change affect globally*/
Console.Write(“{0}\t”,program.X); /* 400 */
show();

}

static void Show()
{
Console.Write(“{0}\t”,program.X); /* 400 */
}

}
}

This program will produce the output:

200 400 400

The static variable is accessed through the class name. A static method can only access and manipulate the static variables.

3. Instance Variables

A field declared without the static modifier is called an instance variable. Instance variables are accessed by using the object of the class. The scope of these variables begins at the place of their declaration and ends when the main method terminates. Instance variables are specific to every object of the class. Every time an object is created, a new instance variable is created. Since instance variables are specific to dynamic objects, they occupy a memory location when an object is created at run time.

Example:

class program
{

int X=200; /* Instance variable */
static void main()
{

program obj=new program();
Console.WriteLine(obj.X);
...

}
}

In this example, the variable X is an instance variable. Therefore, this variable is accessed by creating object obj of program class.

A to Z Full Forms and Acronyms