This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# 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. 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.

int numberOfArticles;

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

boolBoolean flagfalsetrue1
byteUnsigned Byte02558
sbyteSigned Byte-1281278
shortSigned Short Integer-32,76832,76716
ushortUnsigned Short Integer065,53516
intSigned Integer-2,147,483,6482,147,483,64732
uintUnsigned Integer04,294,967,29532
longSigned Long Integer-9x10189x101864
ulongUnsigned Long Integer01.8x101964

Non-Integer (Floating Point) Data Types

floatSingle Precision Number±1.5x10-45 to ±3.4x10387 digits32
doubleDouble Precision Number±5x10-324 to ±1.7x1030815 or 16 digits64
decimalDecimal Number±10-28 to ±7.9x102828 or 29 digits128
27 July 2006