Section 3.3. Variables


3.3. Variables

A variable is an instance of an intrinsic type (such as int ) that can hold a value:

 int myVariable = 15; 

You initialize a variable by writing its type, its identifier , and then assigning a value to that variable.

An identifier is just an arbitrary name you assign to a variable, method, class, or other element. In this case, the variable's identifier is myVariable .

You can define variables without initializing them:

 int myVariable; 

You can then assign a value to myVariable later in your program:

 int myVariable;     // some other code here     myVariable = 15; // assign 15 to myVariable 

You can also change the value of a variable later in the program. That is why they're called variables; their values can vary.

 int myVariable;     // some other code here     myVariable = 15; // assign 15 to myVariable     // some other code here     myVariable = 12; // now it is 12 

Technically, a variable is a named storage location (that is, stored in memory) with a type. After the final line of code in the previous example, the value 12 is stored in the named location myVariable .

Example 3-1 illustrates the use of variables. To test this program, open Visual Studio .NET and create a console application. Type in the code as shown.

WriteLine( )

The .NET Framework provides a useful method for displaying output on the screen in console applications: System.Console.WriteLine( ) . How you use this method will become clearer as you progress through the book, but the fundamentals are straightforward. You call the method, passing in a string that you want printed to the console (the screen), as in the Hello World application in Chapter 1.

You can also pass in substitution parameters. A substitution parameter is just a placeholder for a value you want to display. For example, you might pass in the substitution parameter {0}, and then when you run the program, you'll substitute the value held in the variable myInt , so that its value is displayed where the parameter {0} appears in the WriteLine( ) statement.

Here's how it works. You place a number between braces:

 System.Console.WriteLine("After assignment, myInt: {0}", myInt); 

Notice that you follow the quoted string with a comma and then a variable name. The value of the variable will be substituted into the parameter. Assuming myInt has the value 15, the statement shown previously causes the following to display:

 After assignment, myInt: 15 

If you have more than one parameter, the variable values will be substituted in order, as in the following:

 System.Console.WriteLine("After assignment, myInt: {0} and     myOtherInt: {1}", myInt, myOtherInt); 

Assuming myInt has the value 15, and myOtherInt has the value 20, this will cause the following to display:

 After assignment, myInt: 15 and myOtherInt: 20. 

You'll see a great deal more about WriteLine( ) in later chapters.


Example 3-1. Using variables
 class Values {  static void Main(  )  {  int myInt = 7;  System.Console.WriteLine("Initialized, myInt: {0}",          myInt);  myInt = 5;  System.Console.WriteLine("After assignment, myInt: {0}",          myInt);      } } 

Press F5 to build and run this application; the output looks like this:

 Initialized, myInt: 7     After assignment, myInt: 5 

Example 3-1 initializes the variable myInt to the value 7, displays that value, reassigns the variable with the value 5, and displays it again.



Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net