Data types in C#: integer, Boolean, Real, Char and String with examples

Data types in C#

Integer data types in c#

Let us first discuss integer data types in c# with their characteristics and in particular their limit values. We omit for the moment the bool and char data types in c# which could be qualified as integers but which should be treated separately.

Byte data type variable:

A byte type variable is coded on one byte (8 bits). It can take any integer value included between 0 and 255 (inclusive). Do not use type byte to contain a letter since this is the object of type char. Variables of type byte and sbyte can be used in arithmetic calculations. They are then automatically converted, for the duration of the operation only, into variables of type int.

sbyte data type variable:

A variable of type sbyte is also coded on one byte. It can contain any integer value included between -128 and 127 (inclusive). The s in sbyte is a reminder that this is a signed type.

Short data type variable:

Signed integer encoded on 16 bits. A variable of type short can therefore contain all the integer values ​​included between -215 and 215-1, that is, between -32768 and 32767.

If a variable of type short (but also ushort) is used in an arithmetic calculation, it is automatically converted, for the duration of the operation only, to a variable of type int.

Ushort data type variable:

16-bit unsigned integer. A variable of type ushort can contain all integer values ​​included between 0 and 65535.

Int data type variable:

32-bit signed integer. A variable of type int can therefore contain all the integer values ​​included between -231 and 231-1, that is to say between minus two billion one hundred and forty-seven million (-2 147 483 648 for be precise) and two billion one hundred and forty-seven million. In the number we have just written using digits, spaces have been inserted only to make the number easier to read. These spaces must obviously not be included in a C # program.

Uint data type variable:

32-bit unsigned integer. A variable of type uint can contain all integer values ​​included between 0 and four billion three hundred million (4,294,967,295 to be precise).

Long data type variable

Signed integer encoded on 64 bits. A variable of type long can contain all integer values ​​included between, approximately, -9.2 * 1018 and 9.2 * 1018 (that is, 92 followed by seventeen zeros).

Ulong data type variable

Signed integer encoded on 64 bits. A variable of type ulong can contain all integer values ​​included between, approximately, 0 and 18 * 1018.

To get an idea of ​​what 1019 represents (roughly the highest value of a long), know that you could easily copy in a long number of microseconds (there is one million per second) that have passed since the beginning of our Christian era. Although the subject is only discussed in section 3.4, it should already be noted that integer variables can be considered as class objects (we will then see the interest of such a practice that we calls boxing and unboxing):

Table 1: Classes associated with base types

Type Associated class
byte Byte
sbyte SByte
short Int16
ushort UInt16
int Int32
uint UInt32
long Int64
ulong UInt64

The following two statements are equivalent:

int i;

System.Int32 i;

However, the last form, although strictly identical to the preceding one (in speed and space memory occupied, including during program execution) is obviously used much less, except in programs or parts of programs automatically generated by Visual Studio. Among the properties of these classes, we find MaxValue (the highest value of the type) and MinValue (lowest value). Among the methods, we find especially Parse (which allows to convert a string to a number, see the examples later in this chapter) as well as different other conversion methods. But let’s not anticipate …We declare a variable of types int, short, etc. as in C / C ++:

int i;

int j, k;

int x=Int32.MaxValue;

long y=Int64.MinValue;

int z = 5;

In the last three examples, a variable is declared and initialized immediately. Since the variables i, j and k have not been initialized, they contain random values.

Unsigned types do not conform to CLS:

Let’s already point out (but don’t worry about the problem on the first read) that unsigned data types in c# (ushort, uint and ulong) are implemented in C # but are not necessarily implemented in other languages ​​of the .NET architecture. The CLS (Common Language Specification, which is a standard) does not in fact oblige languages ​​to implement the unsigned qualifier (or its types such as uint or ulong). We say that the ushort, uint, and ulong data types in c# are not CLSCompliant. There will never be a problem if you use these data types in local variables or in private fields or in private classes. Using these data types in c#, as an argument or return value of a function, does not pose no no more problem but on the express condition that the calling function is also written in C #. However, do not use these unsigned C# data types as arguments or return values ​​of public functions that can be called from any language in the .NET architecture. It is also true for public fields or properties of public classes.

Boolean data types in c# :

A Boolean variable can contain the values ​​”true” and “false” (more precisely true or false).

Bool: A variable of type bool can take the values ​​true or false and only these.

A null (zero) value in an integer is not synonymous with false or any value other than 0 synonymous with true. C# is shown, here too, to be much more rigorous than C / C ++. C# obviously associates very specific sequences of 1 and 0 (since everything is 1 or 0 in computer science). true is true and false is false, look no further. A Boolean variable can be considered as an object of the Boolean class, in space of System names.

The real data types in c#:

A variable of real type can contain, with more or less precision, decimal values. The new compared to C / C ++: the decimal type for better precision. We find the C# data types float and double, well known to C and C ++ programmers.

Float data type variable:

Real encoded in single precision, that is to say on 32 bits and in the format defined by the ANSI IEEE 754 standard, later became the IEC 60559: 1989 standard.

The smallest positive real number that can be represented in the float format is approximately 1.4 * 10-45 and the largest 3.4 * 1038. The corresponding negative values ​​as well as the value 0 can also be represented. We can consider that precision is seven decimal places. All combinations of 1s and 0s in a 32-bit area do not necessarily form a number real correct (unlike integers). In case of error, a real number may be in one of the three following states.

  • positive infinity: We have, for example, adding 1 to the largest positive number.
  • negative infinity:  We subtracted 1 from the smallest number.
  • NaN: Not a Number when the combination of 1 and 0 does not match a number real valid.

To test if a float variable contains an erroneous value, you must use the Single class and call methods of this class.

Double data type variable:

Real encoded in double precision, i.e. in 64 bits and in the format also defined by the IEEE standard 754. This is the preferred format for reals. The smallest positive real number likely to be shown in double format is 4.9 * 10-324 and the larger one 1.8 * 10308. The corresponding negative values as well as the value 0 can also be represented.

So as we will explain below, the real numbers in the double format are represented with more precision than real numbers in float format. We can consider that the precision of a double is fifteen decimal places. As with floats, not all combinations of 1 and 0 are allowed. Spend by the Double class and its methods to test whether a variable contains a plausible double value.

Decimal data type variable:

Real encoded on 128 bits as an integer multiplied by a power of ten, which provides accuracy in the machine representation of the number, on condition that it is limited in size (approximately 8 * 1028 still). The decimal type was specially created for financial applications. We will explain below why. Decimal operations are more precise but also ten times slower than operations on doubles or floats.

To get an idea of ​​what 1038 represents (the highest value of a float), calculate the number of microseconds that have passed since the big bang that started the universe: 1023 microseconds only have passed in fifteen billion years.

Variables of type float, double and decimal can be considered as objects respectively of the Single, Double, and Decimal classes of the System namespace.

The char data types in c#:

Compared to C / C ++: similar in appearance but in reality very different. A type variable character (char) is, in fact, encoded on 16 bits, in the Unicode system. This consists of a 16-bit extension of the ANSI code (8-bit code, itself derived from the 7-bit ASCII code) to accommodate has other alphabets (Cyrillic, Hebrew, Arabic, etc.). This switch to Unicode is happy: C / C ++ remained at 8-bit code, which greatly and unnecessarily complicates applications as soon as ActiveX is used (technology, preceding .NET, of independent components languages), automation techniques (control, from a program, of programs like Word or Excel) or simply when exchanging data with programs written in different languages. Today, these technologies require, indeed, generally Unicode (Visual Basic long ago switched to Unicode).

char and byte therefore designate, in C #, different C# data types: coding on 8 bits for byte, and 16 bits for the tank. A variable of type char, like any variable, contains only 1s and 0s. It is only by convention (ANSI codes, Unicode, etc.) that we decide that such a combination corresponds to such letter (for example the value 65 for A). But nothing prevents to consider that it contains an integer value and to perform an arithmetic operation involving this variable (although the short type is then much more appropriate). A variable of type char can be considered as an object of the namespace class Char System In this class, one finds in particular methods making it possible to determine the type of character (letter, number, uppercase, etc.). Like bool, byte, int, long, float, double, and decimal, a char type is of type value: 16 bits (and nothing else) are reserved on the stack for a variable of type char.

Strings data types in c#:

the character arrays of C /C ++ (with their leading zero) and functions of the str family (strcpy, strcat, etc.) which do not perform any checks and have played more than one trick even to the most more attentive.

In C #, a string is an object of the String class of the System namespace. the string is another name for System.String. The String class is rich in functions chain processing. It also contains the Length property which gives the size of the string, in the number of characters. The + operator has been redefined in order to more easily perform string concatenations.

A string is declared as follows:

string s = “Hello”;

Each character in the string is a char and is, therefore, 16-bit encoded in the Unicode system. A string expands automatically as needed (insertions, concatenations, etc.), up to two billion characters, which is more than enough for practical purposes. Let us say already that the character strings of C # can be handled with much more ease and security than those of C / C ++:

string s1 = “Good”;

string s2;

s2 = s1 + “day”;    // s2 contains Hello

s2 + = “Sir.”; // s2 contains Hello Sir

if (s1 == s2) …..   // compare two strings

char c = s1 [0]; // c contains the first character of s1

A variable of type string is used as a variable of type value (like int, long, float, double, and char) although it is a reference type variable (like objects). The compiler thus facilitates the work of the programmer, the typed string being processed as naturally as type int. Thus, by writing:

s1 = s2;

Programming Examples:

How to use integer data types in c#:


Data types in C#

How to use double data type in C#:


Data types in C#

How to use Boolean data types in c#:


Data types in C#

How to use string data types in c#:


Data types in C#

Related Article:

C# Windows Forms Application: How to create First Window Form Application in C#

C# Console Application: How to write First C# Hello World Program

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button