TOC

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

Types de donnée:

Entiers

Les Ordinateurs et les nombres vont de main en main, donc en programmant, vous vous trouverez assez souvent devant de nombreuses formes de nombres. L'une des plus communément utilisées en C#, c'est l'integer. Le mot "integer", c'est le Latin de "entier", ce qui est plutôt logique, parce qu'un integer est un nombre sans partie fractionnaire : Un nombre entier.

Int32 - l'entier par défaut

Comme nous le découvrirons dans peu de temps, le C# vient avec diverses types d'integers, mais celui que l'on utilisera le plus souvent, c'est le "Int32" : Un integer 32 bits. Il peut être déclaré comme ceci:

Int32 number;

Tout de même, puisqu'il est le type le plus utilisé en C#, il a un raccourci - Vous pouvez juste écrire "int" et C# saura automatiquement que vous parlez d'un Int32. Bien sûr, vous pouvez aussi assigner une valeur sur lui, dans la même déclaration, là ou vous la déclarez:

int number = 42;

Remarquez la différence en encadrant comme même - Int32 réfère à une classe, donc il commence avec une majuscule "I", pendant que int est un mot-clé, donc il commence avec une minuscule "i".

Le Int32 est aussi appelée integer, qui peut contenir un nombre dans un rayon de -2,147,483,648 à 2,147,483,647. Vous pouvez vérifier ceci en accédant l'Int.MinValue et MaxValue constants. Si vous avez besoin de nombres plus volumineux, ou si vous connaissez déjà que les nombre que vous travaillerez avec, n'atteindra jamais la limite d'un integer, vous pouvez choisir un autre type.

Types d'Integers

Comme déjà dit, il y a beacoup de types d'integers en C#. En effet, beaucoup trop pour tous les mentionner dans cet article, mais ici sont ceux dont vous rencontrerez de temps en temps le plus communément.

  • byte - un integer non-signé qui peut contenir un nombre entre 0 et 255.
  • short (Ce qui voudrait dire en français, "court".) - Un integer signé de 16 bits, qui peut contenir une valeur entre -32,768 et 32,767. Aussi connu sous son nom formel : Int16
  • long (Ce qui voudrait dire en français, "long".) - un integer 64 bits, qui peut contenir une valeur entre -9,223,372,036,854,775,808 et 9,223,372,036,854,775,807. Aussi sous son nom formel : Int64.

Si vous avez besoin de contenir un nombre encore plus gros que le type long autorise, vous pouvez utiliser un "ulong" à la place - c'est un integer non-signé qui peut contenir une valeur jusqu'à 18,446,744,073,709,551,615. De l'autre côté, puisque c'est un integer non-signé, il n'autorise pas les valeurs négatives. Comme le précédent, vous pouvez trouver des versions non-signées d'autres types d'integer qui fonctionnent de la même façon, comme uint et 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!

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!