Working With Variables in C#
By Stephen Bucaro
Programs usually work with data. When you're working with data in your program, you're usually
working with data that can be changed by the program, as opposed to static or constant data.
This changeable data is referred to as variable. While the program is running, this data is stored in
the computer's memory chips.
The actual binary address of the variable's location in the computer's memory chips is very difficult
to ascertain. Especially in modern computers that use complicated memory management techniques.
In order to maintain access to a given piece of data, the programmer specifies the type of the data,
which tells the computer how to store the data, and gives the piece of data a name.
It then becomes the computer's responsibility to keep track of the variable. The programmer can
always access the data by its name. The act of specifying the type of the data and giving it a name
is called "declaring" a variable. Once you have declared a variable, you can assign it a value. In the
program you can use the variable in calculations and you can change the value of the variable,
but you cannot change the "type" of the variable.
Like other languages, C# variables have "scope". Scope defines which parts of the program
can access the variable. If a variable is declared inside a method, its scope is local to the method
within which it was declared. This means it cannot be referenced in any other method of the class.
If a variable is declared inside a class, its scope is local to the class within which it was declared.
This means it can be accessed from anywhere within that class, but it cannot be referenced by
any method of an other class. Most languages can have variables with global scope. This means
they can be accessed from anywhere in the program. There are no global variables in C#.
There are no global variables in C#, however, the behavior of globals can be achieved by
creating a public static variable in a class and accessing it with Class.Variable = value from
anywhere in the project.
Public Class myClass
{
public static int globalVariable = 5;
}
You can access the variable with myClass.globalVariable
The example code shown below declares a variable of type string named myStr and assigns it a
value of "This is my text". It is a variable with method scope which means it can be accessed from
anywhere within the Main method.
using System;
class localVar
{
static void Main()
{
string myStr;
myStr = "This is my text";
System.Console.WriteLine(myStr);
// wait until user presses Enter key
Console.ReadLine();
}
}
The System.Console.WriteLine is within the Main method so it can access myStr. The Console.WriteLine
is a method to write text or data to the screen. The Console.ReadLine method waits for the user to
press the Enter key to close the program. A program like this that uses Console methods is referred
to as a console program.
To learn how to compile and run thi program without Visual Studio see:
Compile C# Without Visual Studio
|