 .NET 1.1C# Numeric Data Types
The third part of the C# Fundamentals tutorial takes a first look at the numeric data types available to the C# programming language. This article explains how variables are assigned and includes a quick reference to the numeric data types.
Strong and Weak Typed Languages
C# is a strongly typed language. This means that all variables must be declared in your program as being of a specific type. The rules in which the variable behaves are defined by the type that is selected. For example, an integer variable type can only contain whole numbers. If a number in an integer variable is required to have a fractional part, it must first be converted to a different type, possibly being stored in another variable as a part of the translation.
The alternative to a strongly typed language is a weakly typed language. An example would be VBScript used by many classic ASP developers. In this language, the type of the variable is not explicitly declared and the behaviour of the variable can appear to change from one line of code to the next.
Variable Declaration and Assignment
A variable can be declared in a program in a single line of code specifying the variable type and its name, followed by the obligatory semicolon to end the line. In the following code snippet, an integer variable is declared using the data type int.
int numberOfArticles;
A variable can be given a value using assignment operator symbol, (=). The variable to the left of the operator is assigned the value to the right. The following code shows a variable being declared and then being assigned a value.
int numberOfArticles;
numberOfArticles = 3;
A variable does not need to be assigned a value immediately after being created. There may be many lines of code between the declaration of the variable and the assignment of its initial value. However, if you do wish to declare a variable and assign its value at the same time, this can be combined in a single statement as follows:
int numberOfArticles = 3;
It is possible to declare multiple variables of the same type in a single line of code. Similarly if you wish to assign the same value to multiple variables, this can be achieved in a single line of code. Finally, just to complicate things further (or to show the elegance of the C# language depending on your viewpoint), both of these operations can be combined. The following code snippet gives three examples.
// Create multiple variables by separating with commas
int weight, size, quantity;
// Assign the three variable the same value (10)
weight = size = quantity = 10;
// Create three integers and assign their initial values
int weight = 1, size = 2, quantity = 3;
Assignment Problems
Definite Assignment
The C# compiler enforces a rule known as definite assignment. This is a simple rule that states that a variable may not be read from until a value has been assigned to it. This prevents you from writing code that reads a variable that has an undefined value as this can generate unpredictable results.
The following code would fail to compile, instead displaying the error "Use of unassigned variable numberOfArticles".
// Create variable but do not assign a value
int numberOfArticles;
// Try to create a new variable and assign with
// the value from numberOfArticles
int numberToSave = numberOfArticles;
Assignment of Incorrect Type
The second problem that often occurs when assigning values to variables is the attempt to assign a value that is not compatible with the data type declared or would involve loss of information. In the following example the integer variable cannot be assigned a value with a decimal element; the decimal would be rounded. This mistake causes an error in the format "Cannot implicitly convert type 'double' to 'int'".
// Invalid assignment to integer variable
int numberOfArticles = 3.5;
This is not to say that the above conversion is impossible. However, an explicit command indicating that you wish to convert the value's data type is required. Data type conversions will be examined later in the tutorial.
Numeric Data Type Reference
I will end this article with a quick reference to the numeric data types. For integer types, the declaration keyword, a description of the type, the range of possible values and the number of bits used to represent the value are given. For non-integers, the scale of the value and the number of digits of accuracy is given rather than minimum and maximum. This is generally enough information to determine which data type should be used for any numeric variable.
I have included the Boolean type, which although not technically numeric, fits well in this table. The Boolean type is a simple flag that can be set to either true or false. It is named in honour of the mathematician, George Boole.
Integer Data Types
| Type | Description | Minimum | Maximum | Bits |
|---|
| bool | Boolean flag | false | true | 1 | | byte | Unsigned Byte | 0 | 255 | 8 | | sbyte | Signed Byte | -128 | 127 | 8 | | short | Signed Short Integer | -32,768 | 32,767 | 16 | | ushort | Unsigned Short Integer | 0 | 65,535 | 16 | | int | Signed Integer | -2,147,483,648 | 2,147,483,647 | 32 | | uint | Unsigned Integer | 0 | 4,294,967,295 | 32 | | long | Signed Long Integer | -9x1018 | 9x1018 | 64 | | ulong | Unsigned Long Integer | 0 | 1.8x1019 | 64 |
Non-Integer (Floating Point) Data Types
| Type | Description | Scale | Precision | Bits |
|---|
| float | Single Precision Number | 1.5x10-45 to 3.4x1038 | 7 digits | 32 | | double | Double Precision Number | 5x10-324 to 1.7x10308 | 15 or 16 digits | 64 | | decimal | Decimal Number | 10-28 to 1028 | 28 or 29 digits | 128 |
|