C# Tutorial Part 3.1 – Types of Variables

This part of the C# tutorial, still on the topic of variables, is about the different types of variables and how to use them.
Please make sure you have at least read Part 3.0 before reading this.

Please see the full list of C# data types on this web page: http://www.softsteel.co.uk/tutorials/cSharp/lesson4.html
It has a very useful table, giving the details of each data type.

The first column is the name of the type of variable, which is also the keyword used to create one (remember using int to create integer variables last lesson?)
Ignore the second column

The third column says whether the type is signed or not: Signed means it can hold negative values. Notice that uint is identical to int, but not signed. The same goes for other pairs such as ushort and short. Unsigned and signed versions of the same thing can hold the same range of values, but with unsigned, that range begins at zero, rather than having zero at its centre.

The fourth column says the amount of bytes a variable of that type will take up. Bool, byte and sbyte are the smallest.

The fifth column tells you the range of values each type can hold, and for decimal types, their precision in significant figures.

Here are the main types:

int – Holds an integer (whole number value)
double – Holds decimal values
bool – Holds a true or false value instead of a number
string – Holds words or sentences instead of  a number

All variables are declared (created) by using the keyword for the type of variable you want to create, followed by the name of the new variable, then a semi colon. (Declaration of variables was covered in more detail in part 3.0)

double mynewdouble; //Creates a new double variable called mynewdouble
string mystring = “This is what’s in my string” //Creates a new string called mystring and assigns it a string value, using the assignment operator

We’ve covered int variables briefly in the last lesson, and doubles are very much the same, except they can hold decimal values.


Bool is short for boolean, which means it holds values that can only be either true or false. Without wanting to be too patronising, here are some examples of questions, some of which have boolean answers and some which don’t:

Which stop is the bus at?  This question would not have a true/false answer
Is the bus at my stop?  This question would have a boolean answer.

When is it Christmas?    Not boolean
Is it Christmas?    Boolean

Do you prefer red or blue?    Not boolean
Do you prefer red to blue?    Boolean

The kind of things you’re allowed to ask in 20 questions, with Yes/No (true/false) answers – not open ended questions like “How’s life?” or anything.

To assign a boolean variable a value, use the keywords true and false

bool IsThisCSharp = true; //Creates a bool called IsThisCSharp and assigns it the value, true

They may not seem useful at the moment, but they will come into their own when we cover control statements, loops and logic operators in future parts of the tutorial.


Think of lots of letters attached to a piece of string. Strings can be any length, from no letters, to whole sentences. You can write basically anything in a string, but most will be used for storing words the user has inputted for printing later.
The contents of strings must be contained in double quotes, “like this”, and when typed into Visual C#, they will turn red. Remember the quotes when assigning values to a string:

string QuotesReminderString; //Declares a string called QuotesReminderString
QuotesReminderString =
“Remember your double quotes”; //Assigns it a value

The Console.ReadLine method obtains a string input from the user, so it can be used to assign values to strings:

string userinput = Console.ReadLine(); //Declares a string called userinput and lets the user assign it a value by calling Console.ReadLine

Note: Any numbers written in strings count as letters, not numbers, so you could not add a string containing the letters “12” to an int containing the number 4. To do that you would need to convert.

That’s all for this installment. I will publish some questions / challenges about this part and the previous part soon. (It will probably be called Part 3.2)


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 No Category, 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