TOC

This article is currently in the process of being translated into Polish (~15% done).

Data types:

Integers

Komputery i liczby idą ramię w ramię, więc programując, wielokrotnie będziesz pracować z liczbami w różnej formie. Jednym z typów najczęściej używanych w C# jest Integer. Słowo integer pochodzi z łaciny, gdzie oznacza cały, co ma sens: ponieważ integer jest liczbą bez części dziesiętnej - liczbą całkowitą.

Int32 - domyślny typ liczby całkowitej

Tak jak za chwilę wyjaśnimy, C# posiada wiele różnych typów całkowitych, ale tym którego będziesz używać przez większość czasu, jest Int32 - 32 bitowa liczba całkowita. Może być zadeklarowana na przykład tak:

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);

Liczba 50 będzie wypisana w konsoli. Zauważ jak możesz wykonać te obliczenia, nie robiąc nic szczególnego - po prostu określ zmienne i użyj operatora matematycznego i gotowe!

To samo dotyczy innych operatorów matematycznych, i oczywiście możesz użyć liczb, które nie są zdefiniowane jako zmienne:

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!

Summary

Working with numbers, like integers and floating points, is something you will be doing a lot in pretty much any programming task. With your new knowledge about integers and how to work with them, it's time to move on to floating point values in the next article.

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!