Team LiB
Previous Section Next Section


As previously stated, before a variable can be used in a program, the type and name of the variable must be declared. An initial value can be supplied when a variable is first declared, or the variable can be assigned a value later in the program. For example, the following code snippet declares two simple type variables. The first variable, of type int, is given an initial value when the variable is declared. The second variable, of type double, is declared and then assigned a value on a subsequent line of code.

    int count = 3;

    double total;
    // intervening code ... details omitted
    total = 34.3;

A value can be assigned to a bool variable using the true or false keywords.

    bool blah;
    blah = true;

Boolean variables are often used as flags to signal whether or not some code should be conditionally performed. An example follows:

  bool error = false; // Initialize the flag.
  // ...

  // Later in the program (pseudocode):
  if (some error situation arises) {
    // Set the flag to true to signal that an error has occurred.
    error = true;
  // ...

  // Still later in the program:
  if (error) {
        // Pseudocode.
        take corrective action

We'll talk specifically about the syntax of the if statement, one of several different kinds of C# flow control statements, a bit later.

A literal value may be assigned to a variable of type char by surrounding the value (a single Unicode character) in single quotes as follows:

    char c = 'A';

Variable Naming Conventions

Most variable names use what is known as Camel casing, wherein the first letter of the name is in lowercase, the first letter of each subsequent concatenated word in the variable name is in uppercase, and the rest of the characters are in lowercase.


In subsequent chapters, we'll refine the rules for naming variables as we introduce additional object concepts.

For example, the following variable names follow the C# variable naming conventions:

int grade;
double averageGrade;
string myPetRat;
bool weAreFinished;

Recall that, as mentioned earlier, a C# keyword can't be used as a variable name.

int float; // this won't compile--"float" is a keyword

Variable Initialization

In C#, variables aren't automatically assigned an initial value when they are declared, and so we must explicitly assign a value to a variable before the variable's value is accessed in a statement. For example, in the following code snippet, two integer variables are declared named foo and bar. A value is assigned to the variable foo, but not to the variable bar, and an attempt is made to add the two variables together.

int foo;
int bar;

foo = 3; // We're initializing foo, but not bar.
foo = foo + bar; // this line won't compile

If we were to try to compile this code snippet, we would get the following compilation error message regarding the last line of the preceding code example:

error CS0165: use of unassigned local variable 'bar'

The compiler is telling us that the variable bar has been declared, but that its value is undefined. To correct this error, we would need to assign an explicit value to bar before trying to add its value to foo:

int foo;
int bar;

foo = 3;
bar = 7; // We're now initializing BOTH variables explicitly.

foo = foo + bar; // This line will now compile properly.

As it turns out, the story with respect to variable initialization is a bit more complex than what we've discussed here.You'll learn in Chapter 13 that the rules of automatic initialization are somewhat different when dealing with the "inner workings" of objects.

Team LiB
Previous Section Next Section