Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Local & Global Scopes | Methods
C# Basics

Local & Global ScopesLocal & Global Scopes

The scope of a variable is the part of the code where we can access that variable. In C# there is a global scope and many possible local scopes.

When we create a variable outside of any method, it can be accessed from almost anywhere in the program, therefore, it is said to have global scope. For-example:

cs

main.cs

Note

When creating a variable outside any method, we need to add the keyword static before the declaration.

In the above code, we were able to access the variable myVariable both in the Main method and in the testMethod. However, this is not always the case.

A variable declared inside a code-block is only available inside that code block, and the sub-code-blocks, if any. For example, a variable declared inside a method will only be directly accessible inside that method. Similarly, a variable declared inside an if, else if, or else block will only be accessible inside that block. Such a variable is said to have a local scope.

It is a good code reading exercise to look at the following code and try to understand it:

cs

main.cs

In the above code there are four variables:

  • variable_1 - Global Scope;
  • variable_2 - Local Scope, defined in the Main method;
  • variable_3 - Local Scope, defined in the if block;
  • variable_4 - Local Scope, defined in the myMethod method;

The above program may not compile at all but demonstrates where certain variables can be accessed and where they cannot. The area of code where a variable can be accessed is called the scope of that variable.

  • variable_1 is a global variable so it is accessible almost everywhere;
  • variable_2 is accessible throughout the Main method, including the sub-blocks like the if condition;
  • variable_3 is accessible only inside the if block;
  • variable_4 is accessible only inside the myMethod block;

Which keyword do we need to use for global variables (variables declared outside any method) ?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 6. Розділ 8
course content

Зміст курсу

C# Basics

Local & Global ScopesLocal & Global Scopes

The scope of a variable is the part of the code where we can access that variable. In C# there is a global scope and many possible local scopes.

When we create a variable outside of any method, it can be accessed from almost anywhere in the program, therefore, it is said to have global scope. For-example:

cs

main.cs

Note

When creating a variable outside any method, we need to add the keyword static before the declaration.

In the above code, we were able to access the variable myVariable both in the Main method and in the testMethod. However, this is not always the case.

A variable declared inside a code-block is only available inside that code block, and the sub-code-blocks, if any. For example, a variable declared inside a method will only be directly accessible inside that method. Similarly, a variable declared inside an if, else if, or else block will only be accessible inside that block. Such a variable is said to have a local scope.

It is a good code reading exercise to look at the following code and try to understand it:

cs

main.cs

In the above code there are four variables:

  • variable_1 - Global Scope;
  • variable_2 - Local Scope, defined in the Main method;
  • variable_3 - Local Scope, defined in the if block;
  • variable_4 - Local Scope, defined in the myMethod method;

The above program may not compile at all but demonstrates where certain variables can be accessed and where they cannot. The area of code where a variable can be accessed is called the scope of that variable.

  • variable_1 is a global variable so it is accessible almost everywhere;
  • variable_2 is accessible throughout the Main method, including the sub-blocks like the if condition;
  • variable_3 is accessible only inside the if block;
  • variable_4 is accessible only inside the myMethod block;

Which keyword do we need to use for global variables (variables declared outside any method) ?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 6. Розділ 8
some-alt