top of page

Lesson 2 : Variables And Types

We learn how to manipulate values like numbers (integers) and text (strings) within your code.

Lesson Summary:

Variables

All programming languages use varialbes of some sort. Variables are things that allow you to store some sort of value. This value can be anything: a number, a string of text, a "true" or "false" value, and even something more complex like an object representing a list of numbers. This stored value can then be used later in your code.

Types

In C#, each variable has a type. That type defines what kind of value the variable holds. For example, you can say that the type of a variable is "int", meaning the variable can hold an integer (a whole number), or a "string", meaning the variable can hold a string of text (such as "Hello World").

There are tons and tons of types in C#, so many that it'd make little sense to try to list even all the common ones here. Many of the most rudimentary types, such as int and bool, are ofen referred to as primitives, as they are core building blocks for most programs. Types can also be created by the programmer, as we will see in the future when we cover topics such as classes, enums and structs.

Creating and using variables

To create a variable, you specify the type of the variable, then a name for the variable (basically a label, for your own reference). You can also then assign a value of that variable. That value must correspond to the type (integer, string, etc). Don't forget the semi at the end.

Example:

      int myVariable = 5;

      string myOtherVariable = "the value";

I created 2 variables here: one int called "myVariable" with a value of 5, and one string variable called myOtherVariable with the value "the value".

You can also specify a variable create a variable without assigning it a value:

     int myVariable;

When you create a variable (by specifying the type and name, and an optional value), it's called declaring the variable. Giving it a value is called assigning a value. You can assign a value to a declared variable at any time:

    int myVariable;

    myVariable = 5;

    myVariable = 10;

At the end of this code, the value of myVariable will be 10, which is the last value it was set to.

You can also assign a value to a variable based on other variables:

    int num1 = 5;

    int num2 = num1;

The variable num2 will have the same value as num1, which is 5.

You can also have a more complex statement to the right of the equal sign:

    int num1 = 5;

    int num2 = num1 + 3;

The value of num2 will be 8, which is the sum of num1 (5) plus the value 3. See the section on Expressions in this page for more info on this.

Additional Notes and Tips:

More ways of assigning values

I mentioned that you can do things like these with integers:

          num = 5;

          sum = num1 + 5;

          num = num + 1;

          num++;

          ++num;

Along with these, there are other things you can do (using // to denote a comment here)

          num--; // This will subtract 1 from num

          --num; // This will also subtract 1 from num

          num += 5; // This will add 5 to num (changes value of num). Exact same as num = num + 5;

          num += num2; // Will add the value of num2 to num. num2 stays unchanged

          num -= 5; // Subtracts 5 from num

          num *= 5; // Multiplies num by 5

          num /= 5; // Divides num by 5

          sum += (num1 + num2); // Gets the sum of num1 and num2, adds that to sum.

 

There are many other things you can do as well - this only covers some. We will discuss others as they come up.

         

Expressions

The stuff that you typically put on the right of an equal sign on a statement such as 

            int num = num2 + 5;

 

is often called an Expression. Expressions can contain many terms strung together. For example, note the stuff to the right of int num = in the following line:

            int num = 1 + 2 + 3 + 7 - 2 + (5 + 6) + num2 * 3;

 

We see a rather large numerical expression with a bunch of operators like +, -, * and even parentheses. These act just like your typical mathematical operators. 

Expressions need not be to the right of an equal sign. For example, in the lesson we used the following line:

             Console.Out.WriteLine(num1 + 1);

In this case, num1 + 1 is an expression. It is evaluated on the spot (without changing the value of num1), and then that value is output via WriteLine. After that line, the value of the num1 + 1 expression is forgotten unless you set it to a variable, as we showed in the example in the lesson.

Expressions also don't need to be numerical. For example, this is an expression we will learn in the next lesson:

             Console.Out.WriteLine("Some string " + " another string");

 

As we'll learn, this expression combines the two strings into one, and then writes that combined string to the console. You can even combine multiple types into an expression. As we'll soon see, you can use + to append an integer to a string to create a new string which is the combination of both.

Lesson FAQ

What is the difference between num++ and ++num?

They are quite similar. Both increment the value by 1. However, both num++ and ++num also have another property that I didn't mention: they give us the value of num. num++ gives us the value of num BEFORE the increment, and ++num does so AFTER the increment. For example:

   int num1 = 0;

   int num2 = num1++;

   Console.Out.WriteLine(num2);

In this example, the console will say 0, which is the value num1 had before it was incremented by 1. Now see this example:

   int num1 = 0;

   int num2 = ++num1;

   Console.Out.WriteLine(num2);

In this example, the console will say 1, which is the value num1 has after it's incremented by 1.

The difference is subtle and in many situations (such as in our lesson examples) it is not relevant.

Note that we could have rewritten one of the lesson examples as such:

   int num = 1;

   Console.Out.WriteLine(num++);

   Console.Out.WriteLine(num++);

   Console.Out.WriteLine(num++);

   Console.Out.WriteLine(num++);

This would have had the same effect (prints numbers 1 thru 4)... but I felt it was more clear to separate the steps. What would happen if we instead used ++num for each of these? That's right, it would print out 2 thru 5.

Practice

bottom of page