Strong and Weak Typed Languages
C# is a strongly typed language. All variables in a program must be declared as being of a specific type. The variable's behaviour is defined by the chosen type. For example, an integer variable can only contain whole numbers. If a number in an integer variable needs a fractional part, it must first be converted to a different type, possibly being stored in a new 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 VBScript, the type of the variable is not declared and the behaviour of the variable may appear to change from one line of code to the next.
Variable Declaration and Assignment
A variable can be declared with one line of code, specifying the variable type and its name. In the following code, an integer variable is declared using the data type int.
A variable can be given a value using the assignment operator, (=). The variable to the left of the operator is assigned the value to the right. The following code shows a variable being declared and assigned a value.
int numberOfArticles;
numberOfArticles = 3;
You do not need to assign a value to a new variable immediately. There may be many lines of code between the declaring a variable and giving it a value. However, if you do wish to declare a variable and assign a value at the same time, this can be achieved in a single statement. For example:
int numberOfArticles = 3;
It is possible to declare multiple variables of the same type in a single line of code. You can also assign the same value to multiple variables in one statement. To complicate things further (or to show the elegance of C#, depending on your viewpoint), these operations can be combined. The following code shows 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;
NB: Multiple variables can be assigned the same value in this manner because the assignment operation returns the value that has been assigned.
Assignment Problems
Definite Assignment
The C# compiler enforces a rule known as definite assignment. This states that a variable may not be read until a value has been assigned. This prevents you from writing code that reads a variable with an undefined value, as this could give unpredictable results.
The following code fails to compile, 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
A second problem that often occurs when assigning values, is attempting to assign a value that is incompatible with the data type declared, or that 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;
The above conversion is not impossible. However, you must explicitly state that you wish to convert the value's data type. Data type conversions are 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 and the number of digits of accuracy are given, rather than minimum and maximum. This allows you to determine which data type should be used for any numeric variable.
The Boolean type is included, which although not technically numeric, fits well in this table. The Boolean type can hold either true or false. It is named after 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 ±7.9x1028 | 28 or 29 digits | 128 |
27 July 2006