C# Course for Absolute Beginners – Lesson 1.5
In this lab we’ll continue learning the C# programming language, focusing on declaring and using variables.
Step 1: Understanding Basic Algebra
Consider the following basic algebra equation:
5 + X=7
Even if you didn’t know algebra, if you were asked to find the value of X, you would say 2.
Now consider this second equation using that same logic:
X = 7
Y = X + 3
Based on what we know, we can surmise that if we programmed this into C# and selected "Run" that 10 would be printed to the screen. You’ve just read C# on your own. It’s important to understand that reading C# is largely just common sense.
Of course there’s a little more to it. Consider the names for X and Y in C# – these are called variables. They’re like a bucket in your computer’s memory that hold a value. In this case, these buckets are holding numeric values. We can create special buckets for dates and times, for strings of text, for really big numbers, or numbers that have numbers after the decimal point. In our example we expect X and Y to be numeric values. We know that, but we have to use C# to express this to the .NET Framework Runtime. Remember, the Runtime is what actually allocates memory for our data. In our example we have two data items, and we have to tell the Runtime to allocate some space in memory sufficient to hold that data. So we would declare our variables, and to do that we need to first declare our buckets.
Step 2: Declaring Variables in C#
Open the C# project from our previous lab, or start a new project. If you open the previous project, delete the Console.WriteLine and the Console.ReadLine bits that were created earlier.
Declaring our two variables is very easy. Simply type the following lines of code:
"int" is the C# term for integer, a mathematical term referring to a number that has no fractions or decimal values. Remember – our goal in declaring variables is to tell the .NET Framework Runtime to save a place in memory for our numeric values. After that we can access our variables.
Step 3: Assigning Values to the Declared Variables
Now that we’ve declared our variables, we can assign and read the values. Type the following lines of code:
x = 7;
y = x + 3;
The equals sign ( = ) is an assignment operator. In English, that line is basically saying that we set the integer called X to the value of seven. Then notice that we’re sending the value Y into the method WriteLine that we used in the previous labs. We understand that the C# program will know what to do with the integer that we pass in.
When we run the application, we see the number 10 printed to the screen. We can see that our code worked.
Step 4: Creating String Variables in C#
Let’s do something a little different, and create a variable that holds a string (a string holds text). Below the code you just wrote, type the following (Remember that your ReadLine code should still be at the very bottom):
myFirstName = "Bob";
-Notice that after you type the first couple of letters in the second line ( myFirstName = "Bob"; ), IntelliSense appears. IntelliSense knew that you created a new variable called "myFirstName" – all you have to do is hit the equals sign ( = ) and Intellisense types the rest for you.
-When you finish typing the code and press the "Enter" key on your keyboard, the IDE will add formatting and spacing. Again, the IDE will help you format your code to meet the standards that other developers would find acceptable.
Run the application, and you’ll see the number 10 from the first example, and then Bob from the second example.
Step 5: Understanding the Camel Case Convention
The word string is the key word for creating a bucket of memory that can hold textual information. My variables can be named anything I want (within certain prescribed limits) – so we didn’t have to use myFirstName.
As a convention, most programmers do what we did here – lowercase m (my), followed by capital letter at the beginning of each additional word. There’s a capital F for First and a capital N for Name, and this is called camel case. You’ll hear people talking about camel case often. It’s a convention that allows you to more easily read what’s being written. Whenever you create a local variable within a method, you should use camel case as well.
For now we’ll continue to focus on strings and integers, that hold letters and numbers. In the future, we’ll add dates. There are roughly a dozen other options you’ll eventually want to know; we’ll cover those in subsequent labs.
Step 6: Case Sensitivity in C#
Let’s explore case sensitivity. In your code, type the following:
int = X;
X = x + 2;
You should now have two x ints – one upper case, and one lower case. Notice that you don’t receive any clues that there’s an error in the IDE. If you click the "Run" button, you’ll see that your code will run.
You may be wondering, "How can there be two variables – two buckets in memory with the same name?"
The answer is – there aren’t; we have two DIFFERENT variables. C# is a case-sensitive language. Therefore a lower case x and an upper case X are two different buckets in memory. This is a very important concept to understand – when starting with C#, this will often create confusion for people. As you progress, you’ll become more sensitive to this.
Step 7: Avoiding Duplicate Variables in C#
To further demonstrate this point, try to add two lower cases.
(1) Notice that the red squiggly line appears. If you hover over the line you can see that the IDE will give you an option to rename, and will provide you with an explanation of the problem.
To solve this, remove the second lower case x or change it back to an upper case X.
The same issue is true with strings. Try the following experiment:
Copy and paste the string and change "myFirstName" to "MyFirstName"
Notice that because the first letter is different, there are two different variables in memory.
Step 8: Declaring and Assigning Variables in a Single Line
The two lines of string code can also be condensed into a single line. To demonstrate, try the following:
- Delete the following lines of code:
myFirstName = "Bob";
-Type the following line of code:
string myFirstName = "Bob";
Now you have the declaration and the assignment of the variable in one line of code. Experienced developers like to write less code and are always looking for a convenient way to reduce the number of key strokes that they have type. This is one reason why you do this. Another reason you’d want to do this is because you don’t want to create a variable that’s never assigned a value. Typing the code this way initializes the variable with a beginning value – this is a good practice.
Step 9: C# Code Challenge
To close out this lab, we’re going to offer a challenge. Look at the challenge, and see if you can solve it. You’ll find the answer in the beginning of the Day 02 set of labs.
- To start, delete all the code you’ve previously entered. This will allow you a fresh start.
- Type the following code:
int x = 7;
string y = "Bob";
string myFirstTry = x + y;
If you run the application, you’ll see that the console window prints out 7Bob. This is correct, because seven plus Bob equals 7Bob.
Now, add the following code:
int mySecondTry = x + y;
Notice that the red squiggly lines appear. If you hover your mouse over those, you’ll see the message ‘cannot implicitly convert type string to int.’ Your challenge is to try to understand what’s causing that. We’ll provide the answer in the next lab.
In this lab, you learned what a variable is, how to declare a variable and how to initiate a variable. You learned that a variable can be declared and initiated in two separate steps, or combined into a single step. You learned that C# is a case-sensitive language, and that you should use camel casing to improve your code readability. Finally, you’ve been left with a challenge question that will be answered in the Day 02 set of labs.