Let’s talk about variable types

You might remember that I have previously posted about stating a variable in C# (if not, you should definitely check it out here). Well let’s discuss the different types of variables that you may come across while coding.

  • String – this is a variable which would contains letters and maybe numbers. The data is stored using speech marks. For example, if you are storing a name e.g.
string Name = "Jake";
  • Char – if you’re only looking at a single letter then you would store it as a character. Unlike a string, if a datatype is a character then you use single speech marks e.g.
char Letter = 'A';
  • Int – when you are storing only a number then there are a few options. If you want to store a whole number then an integer is the best option. Unlike a string or a char, an integer does not require any sort of speech marks e.g.
int Age = 10;
  • Double – on the other hand, if you want to store a number which is (or could be) a decimal number then it is safer to opt for a double. However, one thing to consider is that an double uses more memory in comparison to an integer so try not to opt for this one as a ‘just in case’ option. Similar to an integer, there is no need for speech marks e.g.
double DecimalNumber = 3.2;
  • Bool – when you are storing a true or false then that’s a simple enough. You would store it as a boolean. While this does contain letters like a string, as it is a boolean then to differentiate it from a string this does not need speech marks e.g.
bool DoILikeCoding = true;

As you may have noticed, when differentiating between datatypes you must remember that speech marks are important. It helps to define the datatype. For example if you need to store the number 4 then:

  • 4 – would be stored as an integer
  • “4” – would be stored as a string

This may seem unimportant at the beginning however if you are wanting to check if two values are equal, but you have stored one as a string and one as an integer, then you could run into errors in your code.

But that’s something I’ll explain further in future posts!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: