DATA TYPES IN C SHARP THE INTEGRAL TYPES
C# data |
.NET data |
Byte |
Minimum value |
Maximum value |
int |
System.Int32 |
4 |
-2.147.483.648
|
2.147.483.647
|
byte
|
System.Byte |
1 |
0 |
255 |
short
|
System.Int16 |
2 |
-32.768
|
32.767 |
long
|
System.Int64 |
8 |
-9.223.372.036.854.775.808
|
9.223.372.036.854.775.807
|
sbyte
|
System.SByte |
1 |
-128 |
127 |
uint |
System.UInt32 |
4 |
0 |
4.294.967.295
|
ushort |
System.UInt16 |
2 |
0 |
65.535
|
ulong |
System.UInt64 |
8 |
0 |
18.446.744.073.709.551.615
|
char |
System.Char |
2 |
0 |
65.535
|
DATA TYPES IN C SHARP THE TYPE CHAR
As shown in the table in the data types in C Sharp the integer types can be signed as int, long, short, sbyte and unsigned byte, uint, ushort, ulong, char. The char type is atypical in that it represents a Unicode character. To declare a char proceed as follows:
char c = ‘A’ ;
The single character must be enclosed in single quotes.
If we use the literal format we can also specify a number in binary notation or a number in hexadecimal notation. Let’s see an example.
BINARY AND HEXADECIMAL NOTATION
int binary = 0b1111;
int hexadecimal = 0x1FB76;
To declare a number in binary form it is necessary to precede the symbol 0b followed by the binary sequence while for a hexadecimal number 0x followed by the hexadecimal sequence. In C# 7.0 the underscore was introduced to separate the digits, it is simply a visual simplification and it is not certain that the underscores separate the thousands, they can be used arbitrarily and then the compiler ignores them because as I told you it is just a visual example. I’ll give you an example.
int x = 123_456_789; ( Underscores will be ignored in memory ).
VISUAL STUDIO CODE FOR INTEGRAL TYPES
using System; namespace IntegralTypes; internal class Program { private static void Main(string[] args) { byte b = 90; //1 byte unsigned da 0 a 255 var x = 120; //4 byte signed da -2.147.483.648 a 2.147.483.647 short y = -90; //2 byte signed da -32.768 a 32.767 long l = 100; //8 byte signed da -9.223.372.036.854.775.808 a 9.223.372.036.854.775.807 Console.WriteLine($"b:{b},x:{x},y:{y};l:{l}"); sbyte sb = -90; //1 byte signed da -128 a 127 uint x1 = 1200; //4 byte unsigned da 0 a 4.294.967.295 ushort y1 = 190; //2 byte unsigned da 0 a 65.535 ulong l1 = 1100; //8 byte unsigned da 0 a 18.446.744.073.709.551.615 var c = 'A'; /*2 byte unsigned da 0 a 65.535 rappresenta un codepoint di un carattere Unicode.*/ //usare int per scrivere un codice binario e esadecimale var b0 = 0b1111; /*per dichiarare un numero in binario basta anteporre 0b al numero binario.*/ var ex = 0x1FB76; /*per dichiarare un numero esadecimale basta anteporre 0x al numero esadecimale.*/ var unicodeString = "\U0001F680"; // 🚀 Console.WriteLine(unicodeString); //INTERPOLAZIONE DI STRINGHE MOLTO PIU' COMPATTA E FACILE DA LEGGERE Console.WriteLine($"sb:{sb},x1:{x1},y1:{y1},l1:{l1},c:{c},b0:{b0},ex:{ex}"); } }
DATA TYPES IN C SHARP I FLOATING POINT
C# data |
.NET data |
Byte |
Interval |
Precision |
float |
System.Single |
4 |
±1.5 x 10 −45 and ±3.4 x 10 38 |
~6-9 digits
|
double
|
System.Double |
8 |
±5.0 × 10 −324 and ±1.7 × 10 308 |
~15-17 digits |
decimal
|
System.Decimal |
16 |
±1.0 x 10 -28 and ±7.9228 x 10 28 |
~28-29 digits |
The float type manages to maintain a precision of approximately 6-9 digits for the decimal part, the double type manages to maintain 15-17 digits of precision for the decimal part. The literal format for floats and doubles is identical, first the integer part, the period and the decimal part.
float myFloat = 20.89f ;
double myDouble = 20.87;
By default, when the C# compiler sees the decimal part in a number, it assigns it to a double, so if we want to declare a float at the end of the number we must add an f. Both the double and the float use the IEEE 754 standard that we have already seen, many microprocessors support this standard and the calculations are very fast, the only problem is that sometimes these data types do not have high precision. To solve the problem, C# introduces the decimal type with 28 digits of precision in the decimal part. This type of data does not comply with the IEEE 754 standard and therefore may be slower from a computational point of view, but it has 28 decimal digits which makes it particularly suitable where high precision is required, for example in financial calculations.
decimal myDecimal = 10.98m ;
The obligatory letter m indicates a money format, i.e. particularly suitable when managing money.
SCIENTIFIC NOTATION
You can use scientific notation for float, double, and decimal types, for example:
double x = 1.8e8; = 1.8×108 = 180000000.0
double y = 1.8e-8; =1.8×10-8 = 0.000000018
BOOLEAN AND STRING TYPE
The bool type only admits the values true or false, i.e. true (1) false (0) occupies 1 Byte.
bool myBool1 = true ;
bool myBool2 = false ;
Strings are not primitive types like those seen so far but are real objects that we will treat separately. For now, it’s enough to know that a string is a sequence of Unicode characters enclosed in double quotes.
string s = “Hello World”;
TYPE INFERENCE
int x = 10 ;
bool b = true ;
float f = 5.9f ;
Until now we have seen that in the declaration of a variable we have always used a primitive data type. In reality the C# compiler is able to infer the type we are declaring by itself by analyzing the literal. Having said that, you can use the var keyword to declare a certain type of data, the compiler will then establish the type via the Type Inference. Having said this, it must be said that type inference worsens the readability of the code, however there are cases (such as in a LINQ query which we will see later) in which the returned type and in these cases the keyword are not known a priori var is very useful. The previous declarations can be replaced with var and you will not have compilation errors.
var x = 10;
var b = true;
var f = 5.9;
VISUAL STUDIO CODE FOR FLOATING POINTS
usingSystem; namespace FloatingPoint { class Program { static void Main(string[] args) { float f = 18.78965f; double d = 987.89; decimal dc = 12.987654323456m; double d1 = 8.1e-8; float f1 = 7.897e12f; //Boolean type which has nothing to do with floating points but which I report here for convenience. //Admits only two values, true or false, true (1) false (0) occupies 1 Byte. bool myBool1 = true; bool myBool2 = false; Console.WriteLine($"f:{f}, d:{d}, dc:{dc}, d1:{d1}, f1:{f1}, myBool1{myBool1},myBool2:{myBool2}"); } } }
FURTHER INFORMATION
In C#, data types can be divided into several main categories: value types, reference types, and nullable types. Here’s an overview of each category and the specific types that are part of it:
Value Types
Value types directly store data and are allocated on the stack. They include simple types, enums, structures, and nullable types.
Simple Types
• Integer Numerics
• byte : 8-bit unsigned integer (0 to 255)
• sbyte : 8-bit signed integer (-128 to 127)
• short : 16-bit signed integer (-32,768 to 32,767)
• ushort : 16-bit unsigned integer (0 to 65,535)
• int : 32-bit signed integer (-2,147,483,648 to 2,147,483,647)
• uint : 32-bit unsigned integer (0 to 4,294,967,295)
• long : 64-bit signed integer (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
• ulong : 64-bit unsigned integer (0 to 18,446,744,073,709,551,615)
• Numeric with Floating Point :
• float : 32-bit single-precision floating point
• double : 64-bit double-precision floating point
• decimal : 128-bit high-precision floating point (specifically for financial calculations)
• Other Simple Types :
• char : 16-bit single Unicode character
• bool : boolean (true or false)
Enums
• enum : A set of named constants.
Structures
• struct : User-defined value types.
Reference Types
Reference types store references to objects, which are allocated on the heap. They include classes, interfaces, arrays, delegates, and the string type.
• Classes :
• class : User-defined reference types.
• Interfaces :
• interface : contract declarations.
• Arrays :
• array : a collection of elements, all of the same type.
• Delegates :
• delegate : pointers to methods.
• Strings :
• string : An immutable sequence of Unicode characters.
Nullable Types
• Nullable : Value types that can also be null. Used to represent value types that can have a null value.
• int? : an int that can be null.
• double? : a double that can be null.
•and so on for other types of value.
Basic Object
• object : The base reference type from which all other types are derived.
Variable Declaration Examples in C#
int numeroIntero = 10; float numeroVirgolaMobile = 3.14f; double numeroVirgolaMobileDoppia = 3.14; char carattere = 'A'; bool valoreBooleano = true; string testo = "Hello, World!"; int? numeroNullable = null; enum GiorniDellaSettimana { Lunedì, Martedì, Mercoledì, Giovedì, Venerdì, Sabato, Domenica } GiorniDellaSettimana oggi = GiorniDellaSettimana.Venerdì; struct Punto { public int X; public int Y; } Punto punto = new Punto(); punto.X = 5; punto.Y = 10;
Leave A Comment