In this lesson, we're going to examine variables and data types more closely. We briefly looked at these coding concepts in Lesson 3 - Building Your First Web App, where it was mentioned that variables are like named “buckets” that store values of the type declared, and that values can be retrieved from. Let’s revisit, and expand on this concept by looking once again at the code within the MyFirstWebApp project.
In the okButton_Click code block, a variable is first declared with an arbitrary name, firstName, and the type of information it that can store, string. The string variable is a type of "bucket" that can hold just enough information for what it holds: alphanumeric characters. firstName is then assigned the string value contained in another variable called firstNameTextBox.Text:
Strings are just one of many, many variable types in C#. If you think of variable types as buckets, as we've explained previously, you can easily visualize the need for a specific type of "bucket" for every data type that needs to be stored. A bucket meant to hold numbers cannot hold letters, for example. C# offers different "buckets" (variable types) for every different purpose.
In the above example, we are completing two separate operations on a single line of code. The first step declares the variable type (what size of "bucket"), as well as its unique name (identifier) that can be used to reference it elsewhere in code. This step simply sets aside an empty “bucket” in memory that is large enough to store a string value, and labels this bucket with a name that uniquely identifies it:
The next step takes that “bucket” and puts a value into it; in this case, an existing string:
You can, of course, write out these steps on separate lines, which would look something like this:
While "name" could be any sequence of alphanumeric characters, we chose the literal string “Bob” to make the two-step process clearer. In this case, we initialize the variable "name" by immediately giving it a literal string ("Bob"), whereas earlier we retrieved the value of another variable and set that value to firstName (this is closer to what you saw in the original example above):
Be careful when declaring variables. It is perfectly acceptable to reference already declared variables, however, you cannot re-declare already declared variables. Here we (1) declared firstName, then (2) referenced it by assigning a value to it, but (3) erroneously re-declared the same variable we declared in (1) To fix this, simply remove the type declaration (string) in (3) and it would be valid C# syntax:
The first two lines of code here can be visualized, using the “bucket” analogy, as looking something like this:
In the above code, you will notice how the variable called “name,“ is later referenced by taking the value that it currently holds (“Bob”) and assigning it, via the equal’s assignment operator, into the variable called “firstName“:
This is a hint as to the versatility you get by creating a variable identifier for referencing throughout your code (such as, in this case, not having to constantly write out the literal string “Bob”). The result of this assignment operation, (taking the value held in name and placing it into firstName) can be visualized as follows:
Note that the “bucket” variable dumping its value into another bucket (via the assignment operator) still holds on to the value itself. In other words, "firstName" can be thought of as copying the value within name, so that "name" and "firstName" both hold the literal string "Bob":
A variable identifier can be labeled as anything you choose, with a few general limitations. The identifier should:
Be at least one alphabetical character.
Not start with a number.
Not contain non-alpha-numerical characters other than dash and underscore.
Not contain any spaces.
Not be exactly the same as a reserved word in C#.
While this might seem like a lot to remember, Visual Studio will tell you when your variable is not named correctly by producing a red squiggly line, indicating an error:
You will also get a green squiggly line, indicating a warning, when you declare a variable that is never made use of. Here, we commented out the line of code that makes use of the variable called "name", leading to a warning:
You can also comment out multiple lines of code, using /* and *\ as follows:
Commenting out code tells the compiler to ignore the line entirely. Commenting can be useful for a number of reasons
- Adding a note to yourself, or others who may read your code.
- Removing parts of your code in order to track down problems.
- Preserving code that you don’t want to throw out entirely, yet has no current use (think code that fulfills some occasional testing purpose, yet shouldn’t be in the final build).
We already know that a string is a sequence of alpha-numerical characters (numbers and letters) but there are many other primitive, pre-defined data types in C# that hold whole numbers, decimal numbers, characters, true/false values, and so on:
The main reason for these different sized “buckets” is generally for sake of economy. In other words, don’t use a bucket large enough to hold a quintillion-sized number (long) when all you need is a tiny bucket, big enough to hold a number up to a few thousand (short). This helps your application to run much more efficiently, and reduces stress on the server and the user's machine.
Here are the default values for uninitialized variables (those that are declared, but not yet given any value by using the assignment operator):
Whole number data types (byte, sbyte, short, ushort, int, uint, long, ulong) – 0
Decimal number data types (float, double, decimal) – 0.0
string data types – “” (empty string)
bool data types – False
char data types – null
This may seem like a rather large list of basic types to keep track of. However, in general the basic data types that you will really need to remember are string, int, double and bool.
Work on remembering the difference between these different data types, and the importance of putting the right information into the right "bucket". You will use variables very often when programming, so knowing these concepts is going to be very helpful to you moving forward. Good job, we'll see you in the next lesson!
Lesson 7 - Variables and Data Types