Data Structures and Algorithms with Object-Oriented Design Patterns in C#
next up previous contents index

Variables

A variable  is a programming language abstraction that represents a storage location. A C# variable has the following attributes:

name
The name  of a variable is the label used to identify a variable in the text of a program.
type
The type  of a variable determines the set of values that the variable can have and the set of operations that can be performed on that variable.
value
The value  of a variable is the content of the memory location(s) occupied by that variable. How the contents of the memory locations are interpreted is determined by the type of the variable.
lifetime
The lifetime  of a variable is the interval of time in the execution of a C# program during which a variable is said to exist. Local variables exist as long as the method in which they are declared is active. Non-static fields of a class exist as long as the object of which they are members exist. Static fields of a class exist as long as the class in which they are defined remains loaded in the C# common language runtime.
scope
The scope  of a variable is the range of statements in the text of a program in which that variable can be referenced.

Consider the C# variable declaration statement:

int i = 57;
This statement defines a variable and binds  various attributes with that variable. The name of the variable is i, the type of the variable is int, and its initial value is 57.

Some attributes of a variable, such its name and type, are bound at compile time. This is called static binding. Other attributes of a variable, such as its value, may be bound at run time. This is called dynamic binding.

There are two kinds of C# variables--local variables and fields. A local variable   is a variable declared inside a method. A field  is a variable declared in some struct or class. (Classes are discussed in Section gif). The type  of a C# variable is either one of the value types  or it is a reference type .




next up previous contents index

Bruno Copyright © 2001 by Bruno R. Preiss, P.Eng. All rights reserved.