In the previous post I presented a reference with the different data types available in Java . I also mentioned the importance of choosing the adequate data type for the information we plan to store on our variables.
The table below shows the primitive data types available in .NET, for you reference.
The primitive data types are defined in the System namespace as structures. This allows the types to have methods in the same way classes do, such as .ToString() or .Parse().
These types are available in all .NET languages since they’re part of the CLR. Each language offers a friendly name for the types, but it represents the same data type.
.NET Type | Description | Range | VB.NET Type | C# Type |
System.SByte | 8-bit signed integer | 128 to 127 | SByte | sbyte |
System.Int16 | 16-bit signed integer | -32,768 to 32,767 | Short | short |
System.Int32 | 32-bit signed integer | -2,147,483,648 to 2,147,483,647 | Integer | int |
System.Int64 | 64-bit signed integer | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | Long | long |
System.Byte | 8-bit unsigned integer | 0 to 255 | Byte | byte |
System.UInt16 | 16-bit unsigned integer | 0 to 65,535 | UShort | ushort |
System.UInt32 | 32-bit unsigned integer | 0 to 4,294,967,295 | UInteger | uint |
System.UInt64 | 64-bit unsigned integer | 0 to 18,446,744,073,709,551,615 | ULong | ulong |
System.Single | 32-bit single-precision floating point with 7 significant figures | +/-1.5 X 10245 to +/-3.4 X 1038 | Single | float |
System.Double | 64-bit single-precision floating point with 16 significant figures | +/-5.0 X 102324 to +/-1.7 X 10308 | Double | double |
System.Decimal | 128-bit high-precision decimal notation with 28 significant figures | +/-1.0 X 10228 to +/-7.9 X 1028 | Decimal | decimal |
System.Boolean | Represents true or false | true or false | Boolean | bool |
System.Char | Represents a single 16-bit Unicode character | Char | char |
Example
using System; namespace Example { class Program { static void Main(string[] args) { byte month = 12; int counter = 0; long sum = 1000000L; double pi = 3.1415926535897932384626433832795; float rate = 4.25F; char letter = 'Z'; bool found = true; //will print 12 Console.WriteLine(month); //will print 0 Console.WriteLine(counter); //will print 1000000 Console.WriteLine(sum); //will print 3.14159265358979 Console.WriteLine(pi); //will print 4.25 Console.WriteLine(rate); //will print Z Console.WriteLine(letter); //will print true Console.WriteLine(found); Console.WriteLine("Press any key to exit"); Console.ReadKey(); } } }
Please note how we use L and F at the end of a number to let the compiler know that the number is a long or a float number instead of int or a double number. This will avoid precision issues.