C# Predefined Primitive Data Types
By Stephen Bucaro
Programs often work with data. This data must be stored in the computer's memory chips or on some non-volatile
storage media, such as a hard disk. A data type defines how much storage a piece of data will take up, the
configuration of that storage, and the meaning of the data. For example a text character will take up 16 bits of
storage that are code for an ASCII or Unicode character. A floating-point number will take up 32 bits of storage,
and will have two parts, the "mantissa," and the "exponent".
Predefined or primitive data types are those defined by the language. These primitive data types can be used
as building blocks for user-defined data types. In addition, primitive data types may be arranged in a row to form
a string data type, or rows and/or columns to form an array data type.
Integer Data Types
Type | Range | Size |
sbyte | -128 to 127 | Signed 8-bit integer |
byte | 0 to 255 | Unsigned 8-bit integer |
char | U+0000 to U+ffff | Unicode 16-bit character |
short | -32,768 to 32,767 | Signed 16-bit integer |
ushort | 0 to 65,535 | Unsigned 16-bit integer |
int | -2,147,483,648 to 2,147,483,647 | Signed 32-bit integer |
uint | 0 to 4,294,967,295 | Unsigned 32-bit integer |
long | -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807 | Signed 64-bit integer |
ulong | 0 to 18,446,744,073,709,551,615 | Unsigned 64-bit integer |
Floating-Point Data Types
Type | Approximate range | Precision |
float | ±1.5e-45 to ±3.4e38 | 7 digits |
double | ±5.0e-324 to ±1.7e308 | 15-16 digits |
Floating point numbers have two parts, the "mantissa," which holds the significant digits, and the "exponent", which
indicates the location of the decimal point. Floating point notation is usually used to define very large numbers. The
"precision" of a floating point number is indicated by the mantissa.
Decimal Data Type
Type | Approximate range | Precision |
decimal | ±1.0 × 10-28 to ±7.9 × 1028 | 28-29 significant digits |
The decimal data type is a 128-bit data type. Compared to floating-point types, the decimal type
has a greater precision (but a smaller range), which makes it suitable for financial and monetary calculations.
Shown below is C#exmple code that declares a variable a of type int and then assigns it a value of 5.
using System;
namespace HelloWorld
{
class UseVariable
{
static void Main()
{
int a;
a = 5;
System.Console.WriteLine(a);
// wait until user presses Enter key
Console.ReadLine();
}
}
}
If you want to compile and run this example, see Compile C# Without Visual Studio
|