C# Tutorial Part 3.0 – Variables

C# sign

Image via Wikipedia


Welcome to the third part of my C# tutorial. This installment will focus on how to create and use variables.   

If you do not know what variables are, a good analogy I have come across to help understand them (in a computing lesson with my teacher Miss Awojinrin) is to imagine a variable being a box, with its name on a sticky label attached to it. The box can hold a value which can be changed and used for certain things. If you want to direct someone to the box, you use the name on the label.
This is very similar to what a variable actually is (obviously, because it’s an analogy). A variable is a location in the computer’s memory, which can hold a value which can be changed and used for certain things. The name of the variable refers to the memory location the values is stored in, but also gives you a name to help you remember what the information being stored there is for.   


C# variables have certain types, so different types of variable hold different types of values. One type of C# variable is int, short for integer. int variables can only hold integer (whole number) values, but can be negative.
In C#, variables are created by saying the type of variable, followed by the name you wish to use for it.  They can be used to count things. To declare (create) a new int variable called ‘counter’, write:   

int counter;   

int is a keyword, so it automatically turns blue. This tells the computer to reserve some memory for an int variable, which will be refered to by the name ‘counter’. Remember the semi colon on the end.
Using the box analogy, this creates a box with a label saying ‘counter’.   

Now that we have created a variable, we can give it a value. This is done using the assignment operator, ‘=’, which copies the value on its right to the variable on its left:   

counter = 10;   

This assigns counter the value 10. (Remember the semi colon)
To save time, variables can be given a value at the same time as being declared. Instead of writing:   

int counter;
counter = 10;   

I could just have written:   

int counter = 10;   

This tells the computer to reserve space for an int variable which will be referred to as counter, and fills it straight away with the value, 10.
Make sure you assign values to your variables before using them.
You don’t just have to give variables constant values like the number 10, you can copy across the values of other variables as well.
Bear in mind, though that to do this, the variables must be the same type – there’s no use copying a string (text) value into an int variable because it won’t work!   

int counter; //Declares the int variable, counter
int othernumber = 12; //Declares the int variable, othernumber and assigns it the value, 12
counter = othernumber; //Copies the value (12) of othernumber to counter, using the assignment operator
Console.WriteLine(“The value of counter is: {0}”, counter); //Displays the value of counter (12) using WriteLine   

That code should all make sense – the declaring of variables from earlier in this lesson, and the Console.WriteLine method from the input lesson. If you’ve forgotten the {0} bit, please recap the previous lesson because it is rather important.   

I’ll leave it at this for part 3.0, but look out for part 3.1 (still on the topic of variables) coming soon, which will look at the other types of variables, and things which can be done with them.   


About Matt

I like writing, filmmaking, programming and gaming, and prefer creating media to consuming it. On the topic of consumption, I'm also a big fan of eating.
This entry was posted in Programming and tagged , , , , . Bookmark the permalink.

Enter comment:

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s