Lesson 1.3 Variable Types

Variables in C# are what are known as type safe. That means that a variable cannot hold just any old value. It has to be of a certain type. Take the variable declaration from the previous example.

  string message = "Hello World!";

It declares a variable called “message” and gives it the value “Hello World”. The prefix “string” is telling C# what type of variable it is. It is a string variable. That means it holds bits of text. If you try putting a number into the variable, it will not compile.

  string message = 10;

That is because 10 is of a different type of value: integer. That is a whole number to most people. Compiling will give the message “Cannot implicitly convert from type ‘int’ to ‘string’”. This is because it cannot reconcile the integer value of 10 into the variable that is supposed to hold a string.

There are many types of basic value that a variable can be. These are known as primitive types. With the exception of four, they all hold different types of number. The first question that might come up is “Hang on; I just need a number, why does a computer need to make the distinction?” The answer comes back to my earlier point. This is the point at which a coder can make a judgement call. It’s the power versus ease of use compromise.

Just as a longer mp3 or bigger picture take more bytes on your hard drive, so bigger numbers take more memory. But here’s where it gets fun. If you have a number with decimal places then the more places you need, the more memory that takes. In fact storing the decimal number 0.12345678901234567890123456789 accurately requires more memory than 2,000,000,000 even though it is a far smaller number! It’s all a matter of being frugal with memory. This is a good practice to get into and good habits are easier to keep than to gain.

If you need to store a numerical value and you are sure it will never go over 250 then it makes sense to save the memory and not use a type that could store a value up to 2 billion. As said, power versus convenience; C# let’s you make the call. But as with all things in life, if you need to make a decision it pays to know a little about your choices.

There are about fifteen different primitive types. To list them all here is somewhat bewildering but it’s worth looking at them as they can be quite useful. Don’t feel you have to memorise them all. Just be aware that there is a choice. Not many coders can recite all of the primitive types, their memory usage and their ranges. Those that do are those who miss the real point of coding: To accomplish a task, not to memorise minutiae in order to get one up. They also tend to be the types of coder who don’t have many friends.

Name Description Bits Range
byte Unsigned integer 8 0 to 255
sbyte Signed integer 8 -128 to 127
int Signed integer 32 -2,147,483,648 to 2,147,483,647
uint Unsigned integer 32 0 to 4294967295
short Signed integer 16 -32,768 to 32,767
ushort Unsigned integer 16 0 to 65535
long Signed integer 64 -922337203685477508 to 922337203685477507
ulong Unsigned integer 64 0 to 18446744073709551615
float Single-precision floating point number 32 -3.402823e38 to 3.402823e38
double Double-precision floating point number 64 -1.79769313486232e308 to 1.79769313486232e308
decimal An integer or precise fractional number that can represent decimal numbers to 29 significant digits 128 ±1.0 × 10e−28 to ±7.9 × 10e28
char A single character 16 The symbols used in text
bool Represents a true or false, on or off, 1 or 0; a logical switch 8 True or false
string A piece of text - -
object Base type of objects, discussed later - -

Quite a selection! They all have varying levels of use and obscurity of use. To go over them all at this point would be to drown you. In my experience there are five that are most frequently used. I'm going to cover these five in the next few sections. By the time you come to use anything more obscure, you should already know why you want it and what it does.