C Sharp/Variables

Lesson Goals
This lesson will focus on the creation and use of different variables in C#. At the end of this lesson you should be able to:
 * Understand what a variable is.
 * Understand the basic C# types.
 * Create variables of different types.
 * Assign values to those variables.
 * Cast from one variable type to another.

Variables
A variable is a place in memory where the application can store some type of information. This information can be a number, a string, a class, a delegate, or even a custom data type.

The primary concept of variables is the naming conventions: For example names like "Hello", "a1234", "_variable", "is_good", "zXYzz" are valid, but "1bac", "C#", "hello world", "invalid.var" are invalid.
 * The only valid characters of a variable is English alphabet (A to Z and a to z), digits (0 to 9) and underscore character (_)
 * The name cannot start by a digit

Types
In the above algebraic example, we have taken for granted that x can actually store a number. Programming languages such as C# have types associated with their variables. Here is a list of all the types in C#.

Variable Instantiation
C# has a common method for the creation of variables. Where type is the "type" of variable to initialize. "name" is a custom name that the programmer assigns the variable. Lets declare our variable x from above. The above code not only creates a variable x that can store a number (as specified by int), but it also stores a value of 5 into that variable. So now whenever we call the variable x the computer will see that as a value of 5.

You can also assign values to your variables at any point in the program. This can be done with the = operator. The above code will create a variable named x that can store an integer number (as specified by int). The initial value of x is zero. However we change the value of x to 5 at the second line of code.

Output
The variable x contains 0 The variable x contains 5 The variable y contains 2 The variable z contains 2.4 The integer cast of z is 2 Hit any key to end...

Remarks
Lets step through this code one line at a time. This line initialized a new integer named x. The initial value of x is set to zero. If we did not define this initial value to zero, then the program would not compile properly. This line outputs some data to the console. This line in particular prints the value of x on the screen. The operator + performs a string concatenation. This means the string value of x is appended to the end of the text and then displayed in the console. Next we've reassigned the value of x. We no longer need the keyword int because we've already told the compiler that we'd like a variable named x. After reassigning the value of x to 5, we then output this to the screen. Next we've created a new variable named y. This variable is initialized to 2. The purpose of this code is to show that your variable can be initialized to whatever you'd like. The result is then output to the console. Here we've created a new variable type. A float can store non-integer numbers. We've initialized this float to 2.4f. The f is to inform the compiler that 2.4f is a floating point value. If we'd used 2.4 instead of 2.4f then the compiler would have assumed that 2.4 represented a double. It would have then thrown an error, because the compiler is not sure how to convert a double to a float without help from the programmer. Here we've casted the floating point value of 2.4f to an integer. This will remove any precision after the decimal place (as we can see on the output). These lines of code pause the execution of the program. Without these lines the program would terminate very quickly, and we wouldn't be able to view the program output. Console.ReadyKey waits until the user presses a key.

Casting
It is often important to be able to convert between different types of data. For example, the user may request a double precision value from your program, but your program stores them as floats. The requires a conversion method between types. This conversion is accomplished via casting. The syntax for casting is as follows: Where type is the type to convert to, and name is the name of the variable to convert.

Output
Original area: 304.12 Truncated area: 304 Hit any key to end...

Remarks
This line of code performs a type cast on the floating point variable named area. It is important to note that a cast does not change the contents of the variable area. Instead, a new integer value of area is created and stored in the variable truncated. The name truncated comes from the fact that this type of conversion (from float to int) removes any precision past the decimal place. This is called truncating.

String
String is any sequence of characters, without limitation of character. String can be empty. Some examples are: In order to convert string to a number (int or double) we use these expressions: For the opposite usage, we use ToString(n). For example:

var keyword
If a variable's type is clear according to the code, we can simply use "var" for its type. For example is these statements:

But this statement is wrong: because the compiler cannot understand what type of variable it is. Also, you cannot define a field (variable of a class) by var.

Summary
(type)name
 * Variables are used to store different types of data in C#.
 * C# has many types. Commonly used types include int, float and bool.
 * Each variable is given a unique name by the programmer. The name should reflect the type of data that is being stored.  Words such as Area and IsValid are more descriptive than t_var1e.  Using descriptive variable names can help with code comprehension.
 * Casting between basic types is possible using the syntax
 * Casting can introduce problems if the destination data type causes a loss of information. An example is when converting from a float to an int.  The float will be truncated to remove any precision after the decimal place.  This causes a loss of data and it is important that the program be robust enough to deal with the loss of data accordingly.

Practice Exercises

 * Create a program that can convert a double to an int and then back to a double. What is the final value of your variable?  Did you expect this outcome?  Make lots of explanatory console outputs so that you can better understand what is going on at each step of your code.
 * Create a program that converts from an int to a char. What happens when you output this char to the screen using Console.WriteLine?