Part 2 - Variables
Part 2 of this C# tutorial series is all about variables.
Overview
Variables are keywords that denote storage space for a program to store various data. The amount of space that is given depends on the type of the variable.
The most basic form a variable declaration is shown below:
<data type> <variable list>
If you want to get fancy you can add an additional access modifier. Access modifiers denote who has access to a variable/function, but we will cover that in detail in another tutorial.
<access modifier> <data type> <variable list>
There are two main types of variables; Value and Reference types.
Value Types
Value type variables can be assigned a value directly. When you declare these variables, the system allocates the appropriate amount of memory for them. The table below illustrates many of the value types available in C#.
Reference Types
Reference types do not contain data directly, they simply give a reference, or location, to data. One may ask why you would want references to variables rather than all variables being value typed. The reason is that if the data inside a reference variable changes, the reference variable automatically reflects those changes. This makes more sense as you start to delve into complex data types.
A common example is creating an instance of a class, and then setting that instance to a known instance of the class. By doing that, you have told the second instance of the class to point to the original.
Example of a reference type:
string name = "Drew"
Example of a reference type:
string name = "Drew"
Example
Video