C sharp (C#)

C# Data Type: Integer, Boolean, Char, and Real

Data types in C#:

C# Data type– The value type variables defined in a function each occupy a few bytes (this depends on the c# data types of the variable, see below) in a memory area, called a stack. This is a program memory area reserved for storing local variables, arguments functions, and return addresses), associated with the program. Various microprocessor instructions give optimized access to the stack, which is not the case for the heap which is the memory area where the objects are allocated. C# precisely defines the size of variables of a given data type: when the program is running, when entering a function, memory is reserved for the variables of the function (more precisely on the stack, in the case of value type variables). For each variable of the function, n bytes are reserved, where n depends on the c# data types.

An integer variable of c# data types int is always encoded on 32 bits, and one of c# data type long on 64 bits. This is not the case in C / C ++ where an int can be encoded, depending on the machine or the operating system, 16- or 32-bit (typically, but could be any number of bits). The C and C ++ standards leave the choice of the number of bits to the compiler implementer. That C# precisely defines the size of the memory area allocated to each c# data type is excellent. thing because it ensures natural compatibility with other languages ​​of the .NET architecture, they also subject to the same constraints. So int is encoded the same way, on 32 bits, too both in C# and in VB.NET, and even in any “.NET compatible” language, present or future. This is which allows, in particular, to use in VB.NET functions created in C#. Nothing to do with the JavaScript var, it is the compiler that determines the c# data type of a variable according to its initialization value and this at compile time. The c# data type of this variable cannot then change during execution. C# version 4 introduced the dynamic type, which allows you to vary the type of running program.


allpcb circuit

Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Integer c# data type:

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

Byte:

A byte c# data type variable is coded on one byte (8 bits). It can take any integer value included between 0 and 255 (inclusive). Do not use c# data type byte to contain a letter since this is the object of type char. Variables of c# data 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 c# data type int.

sbyte:

A c# data 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:

Signed integer encoded on 16 bits. A c# data 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 c# data 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:

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

Int:

32-bit signed integer. A variable of c# data 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 to 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:

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

long:

A 64-bit signed integer. A variable of c# data 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:

A signed 64-bit integer. A variable of c# data type ulong can contain all integer values ​​included between, approximately, 0 and 18 * 1018.


Classes associated with base types:

Type Associated class
Byte Byte
short Int16
int Int32
long Int64
sbyte SByte
ushort UInt16
uint Int32
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 ++:

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. We go will congratulate, the compiler also forbids that variables be used to the right of an assignment without having been initialized.



Unsigned c# data types do not conform to CLS:

Let’s already point out (but don’t worry about the problem on the first read) that unsigned c# data types (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 types ushort, uint and ulong are not CLS-compliant. There will never be a problem if you use these c# data types in local variables or in private fields or in private classes. Using these c# data types, as an argument or return value of a function, does not pose any 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. This is also true for public fields or properties of public classes.

The boolean c# data type:

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

bool:

A variable of c# data 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) but it remains from the internal kitchen to C#. 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.

Real c# data types:

A variable of real c# data 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 types float and double, well known to C and C ++ programmers.

Float:

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:

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:

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 c# data type:

Compared to C / C ++: similar in appearance but in reality very different. A c# data 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, Devanagari, 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 types: coding on 8 bits for byte, and 16 bits for the tank. A c# data 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 a 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 c# data 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 c# data type is of type value: 16 bits (and nothing else) are reserved on the stack for a variable of type char.

Character strings:

Compared to C / C ++: paradise! Forget, and with what happiness, the character arrays of C / C ++ (with their leading zero) and the functions of the str family (strcpy, strcat, etc.) which do not perform any checks and have played more than one trick even to the programmers the more attentive. In C#, a string is an object of the String class of the System namespace 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 a 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. We will come back to strings several times in this article. Let us say already that the character strings of C# can be handled with much more ease and security than those of C / C ++:

A c# data 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 type string being processed as naturally as type int. Thus, by writing:

s1 = s2;

we copy the contents of s2 into s1.


The qualifier const:

Any variable can be called const. In this case, the variable must be initialized but cannot be modified later (by a program instruction). As in C ++, this qualification makes it possible to ensure that a variable is not inadvertently modified (generally an omission of the part of the programmer):

const int N = 10;

A constant can be initialized from the content of another constant:

const int n1 = 3;

const int n2 = n1;

const int n3 = n1 + 2;

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Related Articles

Leave a Reply

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

Back to top button