0.4.6 How to Assign Primitive Values

The five types that you will see often in C# are bool, int, double, decimal, and string. Custom types also exist and will be covered in a future document.

These five types are called primitive, atomic, or basic types. The differences between them will be established at a later point. Right now the focus will be on how to set values of them.

These are examples of how these types are assigned. A bool must be lowercase true or false. Decimals have an “m” at the end of them to denote that they are used as money.

The concept of types of data is one that is pretty pervasive across computing and other programming languages.

So when should each of these types be used?

Bools are used when asking questions that can be answered with true or false, yes or no. Is it raining?

If you ask the height of something, or anything that isn’t a yes/no question, bool is an incompatible type.

An int is used when you’re counting things.

Integers have no decimal places, you can see the Example in the right of the above image that it is a whole number.

A new column will be added here, to show examples of variables that would be of these types. You want to name your boolean variables in such a way that you know what they’re meaning. The names of the variables in the following image will lead you to believe they can be answered with true or false:

Where as with int, you would want to make the variable name evident that you are counting something:

How many times did I do something? How many times did this happen? How many of these do I have? These are the kind of questions that can be answered by the int variable.

A double is used for measuring things. Doubles do have decimals, so therefore are used for more precise and specific numbers.

Comparing between ints and doubles, look at this image of marbles:

I cannot have 5.25 marbles, I can have whole marbles, or none of them. But I can measure something specifically to find an exact decimal of what I am measuring. With tools like micrometers you can measure to very specific levels.

Decimals are for things that are very large and also need to be very precise. There is a site called USE Debt Clock where you can see the US National Debt. The number for the US National Debt is very large and very specific.

This number is constantly increasing, and will be different to the above if you were to look at it now. A decimal would be an appropriate type to be have a variable such as this.

Strings are simply for alphanumeric data. If you wanted to put in your name, or your address, your country, anything else like this, you could use a string.