Working with Variables


Now that you've been introduced to the most basic C# program, it's time to declare a local variable. Once a variable is declared, you can assign it a value, replace that value with a new value, and use it in calculations, output, and so on. However, you cannot change the data type of the variable. In Listing 1.9, string max is a variable declaration.

Listing 1.9. Declaring and Assigning a Variable

Beginner Topic: Local Variables

A variable is a symbolic name for a storage location that the program can later assign and modify. Local indicates that the programmer declared the variable within a method.

To declare a variable is to define it, which you do by

  1. Specifying the type of data to which the variable will refer

  2. Assigning it an identifier (name)


Data Types

Listing 1.9 declares a variable with the data type string. Other common data types used in this chapter are int and char.

  • int is the C# designation of an integer type that is 32 bits in size.

  • char is used for a character type. It is 16 bits, large enough for (nonsurrogate) Unicode characters.

The next chapter looks at these and other common data types in more detail.

Beginner Topic: What Is a Data Type?

The type of data that a variable declaration specifies is called a data type. A data type, or simply type, is a classification of things that share similar characteristics and behavior. For example, animal is a type. It classifies all things (monkeys, warthogs, and platypuses) that have animal characteristics (multicellular, capacity for locomotion, and so on). Similarly, in programming languages, a type is a definition for several items endowed with similar qualities.


Declaring a Variable

In Listing 1.9, string max is a variable declaration of a string type whose name is max. It is possible to declare multiple variables within the same statement by specifying the data type once and separating each identifier with a comma. Listing 1.10 demonstrates this.

Listing 1.10. Declaring Two Variables within One Statement

string message1, message2;

Because a multivariable declaration statement allows developers to provide the data type only once within a declaration, all variables will be of the same type.

In C#, the name of the variable may begin with any letter or an underscore (_), followed by any number of letters, numbers, and/or underscores. By convention, however, local variable names are camel cased (the first letter in each word is capitalized, except for the first word) and do not include underscores.

Assigning a Variable

After declaring a local variable, you must assign it a value before referencing it. One way to do this is to use the = operator, also known as the simple assignment operator. Operators are symbols used to identify the function the code is to perform. Listing 1.11 demonstrates how to use the assignment operator to designate the string values to which the variables max [1.7] and valerie will point.

[1.7] I am not using max to mean the math function here; I'm using it as a variable name.

Listing 1.11. Changing the Value of a Variable

classMiracleMax {   static void Main()   {       string valerie;       string max = "Have fun storming the castle!";                              valerie = "Think it will work?";                                           System.Console.WriteLine(max);       System.Console.WriteLine(valerie);       max = "It would take a miracle.";                                          System.Console.WriteLine(max);   } } 

From this listing, observe that it is possible to assign a variable as part of the variable declaration (as it was for max), or afterward in a separate statement (as with the variable valerie). The value assigned must always be on the right side.

Running the compiled MiracleMax.exe program produces the code shown in Output 1.3.

Output 1.3.

 >MiracleMax.exe Have fun storming the castle! Think it will work? It would take a miracle.

C# requires that developers assign a local variable before accessing it. Additionally, an assignment returns a value. Therefore, C# allows multiple assignments within the same statement, as demonstrated in Listing 1.12.

Listing 1.12. Assignment Returning a Value That Can Be Assigned Again

classMiracleMax {     // ...     string requirements, max;     requirements = max = "It would take a miracle.";     // ... }

Using a Variable

The result of the assignment, of course, is that you can then refer to the value using the variable identifier. Therefore, when you use the variable max within the System.Console.WriteLine(max) statement, the program displays Have fun storming the castle!, the value of max, on the console. Changing the value of max and executing the same System.Console.WriteLine(max) statement causes the new max value, It would take a miracle., to be displayed.

Advanced Topic: Strings Are Immutable

All data of type string, whether string literals or otherwise, is immutable (or unmodifiable). For example, it is not possible to change the string "Come As You Are" to "Come As You Age". A change like this requires that you reassign the variable to point at a new location in memory, instead of modifying the data to which the variable originally referred.





Essential C# 2.0
Essential C# 2.0
ISBN: 0321150775
EAN: 2147483647
Year: 2007
Pages: 185

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