What is Data Type, Scope, Accessibility, Lifetime and Visibility of a Variable

Data Type determines the kind of the data the variable can hold (integer, character, string, and so forth).

Scope defines the code that can access the variable. For example, if you declare a variable inside a for loop, only other code inside the loop can use the variable. If you declare a variable at the top of a method, only the code in the method can use the variable.

Accessibility determines what code in other modules can access the variable. If you declare a variable inside a class at the class level (outside of any method in the class) and you use the private keyword, only the code in the class (or derived classes) can use the variable. In contrast, if you declare the variable with the public keyword, code in other classes can use the variable, too.
Lifetime determines how long the variable’s value is valid. For example, a variable declared inside a method is created when the method begins and is destroyed when it exits. If the method runs again, it creates a new copy of the variable and its value is reset.

Visibility is a concept that combines scope, accessibility, and lifetime. It determines whether a certain piece of code can use a variable. If the variable is accessible to the code, the code is within the variable’s scope, and the variable is within its lifetime (has been created and not yet destroyed), then the variable is visible to the code.


Source: C# 5.0 Programmer’s Reference

Leave a Reply

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