TOC
Data types:

Integers

Computers and numbers go hand in hand, so when programming, you will very often find your self working with numbers in many forms. One of the most commonly used types in C# is the integer. The word "integer" is Latin for "whole", which makes sense, because an integer is a number with no fractional part - a whole number.

Int32 - the default integer

As we'll discuss in a short while, C# comes with many different integer types, but the one you will likely be using most of the time, is the Int32 - a 32 bit integer. It can be declared like this:

Int32 number;

However, since this really is the most used integer type in C#, it has a shortcut - you can just write "int" and C# will automatically know that you're talking about an Int32. You can of course also assign a value to it, in the same statement where you declare it:

int number = 42;

Notice the difference in casing though - Int32 refers to a class, so it starts with an uppercase I, while int is a keyword, so it starts with a lowercase i.

The Int32 is a so-called signed integer, which can hold a number in the range of -2,147,483,648 to 2,147,483,647. You can verify this by accessing the Int32.MinValue and Int32.MaxValue constants. If you need larger numbers, or if you already know that the numbers you will be working with will not ever reach the limits of an integer, you can choose another type.

Integer types

As already mentioned, there are a lot of integer types in C#. In fact, too many to mention them all in this article, but here are the most common ones that you will likely run into from time to time:

  • byte - an unsigned integer which can hold a number between 0 and 255.
  • short - a 16 bit signed integer, which can hold a value between -32,768 and 32,767. Also known under its formal name Int16.
  • long - a 64 bit integer, which can hold a value between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. Also known under its formal name Int64.

If you need to hold a bigger number than the long type allows, you can use a ulong instead - it's an unsigned integer, which can hold a value up to 18,446,744,073,709,551,615. On the other hand, because it's an unsigned integer, it doesn't allow negative values. Likewise, you can find unsigned versions of the other integer types which works the same way, like uint and ushort.

Math with integers

When working with integers and numbers in general, you might be looking to do some calculations. This comes very easy in most programming languages, including C#, where you can use all the most common mathematical operators as they are. For instance, you can do a calculation like this:

int a = 42;  
int b = 8;  

Console.WriteLine(a + b);

The number 50 will be printed in the console. Notice how you could deal with this calculation without doing anything special - just specify the variables and the mathematical plus operator and you're good to go.

The same goes if you want to use other mathematical operators, and you can of course use numbers which are not already defined as a variable in there as well:

int a = 42;  
int b = 8;  

Console.WriteLine(200 - ((a + b) * 2));

Notice the extra set of parentheses - just like in regular math, you will have to deal with the order of operations.

Integers and division

Now that we have added, subtracted and multiplied, let's talk a bit about division. Consider the following example:

int a = 10;
int b = 3;

Console.WriteLine(a / b);

You can probably calculate that in your head and so you know the result should be 3.33333333333. But if you run the example, you will see something strange - the result is instead 3. The reason is that if you do a division between two integers, C# will also return an integer, and since an integer can't contain any fractions, the result is just rounded (in this case down to 3). So if you want a result with fractions, you should either declare one of the variables as a floating point variable (more on those later) or typecast one of them on the fly:

int a = 10;
int b = 3;

Console.WriteLine((float)a / b);

Type conversion/casting

Let's talk more about how you can convert from and to an integer. First of all, C# allows for implicit and explicit conversions between various integer types. An example of an implicit conversion could look like this:

int a = 10;
long b = a;

In other words, if you have an integer, you may treat it as a long. This is possible because C# knows that the value of an integer can fit inside a long. On the other hand, the other way around might not be true - therefore, you will have to use an explicit conversion for that:

long a = 10;
int b = (int)a;

This is of course to make you aware that you are doing something that potentially could go wrong - if you have a long value which exceeds the capacity of a regular integer, you're in trouble!