Now that we have described C# variables and constants, it is time to start looking at the different types that are available to us as C# programmers. This chapter will begin by looking at numerical data types.
C# Integer Types
Perhaps the most widely used of the data types is the integer. C# provides several different integer types based on number size and whether the integers are signed (positive or negative) or unsigned (positive only). All the integer variable types have one thing in common: they may only be used to store whole numbers.
The following table lists the various C# integer types, details of the number of bytes of physical memory consumed by each type, and the acceptable value ranges.
Type | Size in Bytes | Value Range |
---|---|---|
byte | 1 byte | 0 to 255 |
sbyte | 1 byte | -128 to 127 |
short | 2 bytes | -32,768 to 32,767 |
ushort | 2 bytes | 0 to 65,535 |
int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
uint | 4 bytes | 0 to 4,294,967,295 |
long | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
ulong | 8 bytes | 0 to 18,446,744,073,709,551,615 |
The following code demonstrates some of these data types:
int myInt = 2010210; byte myByte = 233; sbyte mySbyte = 127;
All C# numerical data types contain bounds properties that can be accessed to identify that particular type’s minimum and maximum supported values. The following code, for example, outputs to the console the minimum and maximum bounds for some of the integer types listed above:
System.Console.WriteLine("byte Min Value = " + byte.MinValue); System.Console.WriteLine("byte Max Value = " + byte.MaxValue); System.Console.WriteLine("short Min Value = " + short.MinValue); System.Console.WriteLine("short Max Value = " + short.MaxValue); System.Console.WriteLine("int Min Value = " + int.MinValue); System.Console.WriteLine("int Max Value = " + int.MaxValue); System.Console.WriteLine("uint Min Value = " + uint.MinValue); System.Console.WriteLine("uint Max Value = " + uint.MaxValue); System.Console.WriteLine("long Min Value = " + long.MinValue); System.Console.WriteLine("long Max Value = " + long.MaxValue);
Output:
byte Min Value = 0
byte Max Value = 255
short Min Value = -32768
short Max Value = 32767
int Min Value = -2147483648
int Max Value = 2147483647
uint Min Value = 0
uint Max Value = 4294967295
long Min Value = -9223372036854775808
long Max Value = 9223372036854775807
C# Floating Point Variables
Integers are fine for dealing with whole numbers but of little use when there are numbers after the decimal point. Such numbers may be stored in float
or double
variable types. The default type for such numbers is double
. The following table shows the two types with comparisons of the number ranges supported and the number of significant digits in each case:
Type | Size in Bytes | Value Range | Digit Accuracy |
---|---|---|---|
float | 8 bytes | +/-1.5 * 10−45 to +/-3.4 * 1038 | 6 – 7 digits |
double | 16 bytes | +/-5.0 * 10−324 to +/-1.7 * 10308 | 15 – 16 digits |
Since floating point values in C# are considered to be double
by default, it is not possible to declare a float
variable as follows:
float myFloat = 23234.23
An attempt to compile the above code will result in a syntax error that reads in part:
Literal of type double cannot be implicitly converted to type `float'
First, we need to understand what literal means in this context. Any value that is explicitly entered into C# code, such as a number (10, 23234.34, -11, etc.) or a string (“Hello World”, “Dog” etc.), is referred to as a literal.
In the above example, a literal value of 23234.23 is assigned to a float variable named myFloat. The problem is that because floating-point literals are considered to be of type double
this assignment cannot be made because the types don’t match. Instead, we have to tell C# that our literal number is actually of type float
. This is achieved by placing an ‘f’ suffix at the end of the number literal as follows:
float myFloat = 23234.23f
The following code demonstrates float
and double
declarations:
float myFloat = 312.20f; double myDouble = 1856675407371955269.95;
Note: It is important to note that float
and double
variables cannot be used as counting variables (for example, in looping constructs).
C# Decimal Type
Both the integer and floating point families of C# variable types have some limitations. Integers can only handle whole numbers, resulting in the fractional part of a value being stripped off. Floats, on the other hand, have problems with rounding accuracy. Clearly, the best of both worlds is sometimes needed, and the decimal
variable type is provided to address this requirement. The decimal
type is a compromise between integer and float variable types in that it can store fractional parts of a value and provide exact values in computations.
As with floating point numbers, literal values must be declared as being of type decimal
using an m
suffix. The following code declares two decimal
values, multiplies them, and displays the result:
decimal val1 = 11231.582m; decimal val2 = 43342.12m; decimal result = val1 * val2;
C# Boolean Type
The C# Boolean variable type is declared using the bool
keyword and allows for the storage of true
and false
values. Boolean variables are particularly useful in flow control constructs such as if
and while
statements.
Unlike some other programming languages, C# Boolean variables must be assigned either true
or false
and cannot be assigned 1 or 0:
bool loopFinished = false; if (loopFinished) { System.Console.WriteLine("The loop is complete"); } else { System.Console.WriteLine("Still looping"); }
Output:
Still looping