Five Minute C# — Lesson 2

Dealing with different types of data

Alexander Laurence
Five Minute C#
5 min readOct 28, 2018

--

Lessons: 12345678910 ・ 11 ・ 12 ・ 13 ・ 14

Numbers

We previously looked into strings which only contained letters, but what about numbers?

A string holds alphanumeric data, so it is more than happy to be fed numbers as well as letters.

You can even include spaces in strings!

However, if you do enter your number it will consider it as a text. I’ll show you what this means… Let’s concatenate some strings!

Rather than giving us the total score, the output will show us this:

Since we declared the variables player1_score and player2_score as strings, it will treat our data entirely as if it was a block of text… This is not what we want…

So, let’s move on to integers!

In some cases, numbers need to be treated as their own type. When you are dealing with integers (whole numbers that can be positive, negative, or zero), you can declare your variable as the type int (just like with strings). For example…

And unlike strings, you can now perform arithmetic! Let’s try addition.

You could even made a+b into its own variable c

With both methods, you’ll still get the same output. But what I hope to show you is that there are more than one way of thinking about problems and writing solutions.

OK! So we know how to add integers, what else?

If you want to subtract, use the - operator.

Which will output:

If you want to multiply, use the * operator.

Which will output:

And finally, if you want to divide, use the / operator.

Which will output:

Great… but you might be wondering, why did it not return 0.5? Well remember, that’s because we declared c as an integer. Integers can only take whole values. In this case, we are dealing with fractions…

Let’s move on to floats, doubles, and decimals! This might get a bit tricky, but it’s really quite simple! You see, floats, doubles, and decimals all deal with non-integers (numbers that are not whole).

A float stores 32-bit floating point values, a double stores 64-bit floating point values, and a decimal stores 128-bit floating point values. But what does this all mean?

For beginners, you needn’t worry about the differences too much. But since you are all so curious: Float uses less space and has the lowest accuracy/precision. Decimal uses the most space and is the most accurate, but it’s also quite a bit more expensive in processor time too as it is not an intrinsic type. One advantage of Decimal is that it is optimized for financial calculations (where only two decimal points are significant).

But let’s focus only on floats for now. We can declare a float variable just like integers. But unlike integers, we must include the ‘f’ suffix to indicate it is a float.

So let’s try that using division once again, but let’s declare c as a float type:

If we run this, this is what we will get:

Still zero? What went wrong?! Well… Remember, a and b are still integers. Even if c will store float values, we can explicitly convert it to a float (casting).

This will finally give us our answer.

Let’s put our knowledge to the test!

You have a job to crunch the numbers of attempts at goal during a football match. Consider the following variables:

Now let’s input the values and try a bit of arithmetic! The team has shot a total of 7 times, 5 of which were off target, 3 of which were on target. Oh boy…

What would your shot_accuracy float look like if you wanted to calculate the accuracy of the shots between shots_offTarget and shots_onTarget?

Answer:

Output:

Phew. That was a lot to chew on today. That’s all for now!

--

--

Alexander Laurence
Five Minute C#

Alex graduated from the University of Edinburgh with a Masters degree in Neuroscience. He spent 3 years teaching and now runs his own tech venture.