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 Literals

The fourth part of the C# Fundamentals tutorial discusses the C# numeric literals. These are fixed numbers that can be assigned to numeric type variables. Literals as a development topic seems simple but there are some important rules.

What is a Numeric Literal?

A numeric literal is a fixed number that may be assigned to a variable or used in calculations. Literals are sometimes known as constants. However, this should be avoided as there are other types of constant in C#. Here are some examples of literals being assigned to variables:

int facesOnADie = 6;        // 6 is a literal
int lessThanZero = -1;      // -1 is a literal
int zeroValue = 0;          // 0 is a literal
double piApprox = 3.142;    // 3.142 is a literal

Literal Data Types

Numeric literals have data types in the same way that variables do. This can cause unexpected errors when compiling code. For example, the literal value, 123.45, is of the double data type. If you try to assign this literal to a variable defined as a float, the compilation will fail.

float unitPrice = 123.45;      // This will not compile

The problem here is that you cannot always implicitly convert a double (123.45) to a float. The C# compiler understands this and prevents compilation. To avoid this, it is important to understand how C# determines the data type of a literal.

How C# Allocates Numeric Literal Data Types

C# employs some simple rules to determine the type of a numeric literal. Firstly, the literal is checked to see if it is an integer or if it has a decimal point. For integers, the smallest possible data type for the value is selected. This does not include short or ushort so an integer literal will be an int, uint, long or ulong.

Where a literal includes a decimal point, the data type is double. This data type provides the largest range of possible floating-point values but not the greatest accuracy.

Overriding Numeric Literal Data Types

The rules that C# uses to determine the data type for a literal are satisfactory in many situations. However, there are times when the desired data type is different to that selected. In these cases, a suffix can be added to the value to tell the compiler to use a different type. For example, when allocating a literal to a float variable, the 'F' or 'f' suffix is used.

float unitPrice = 123.45;     // This will not compile
float unitPrice = 123.45F;    // This will compile
float unitPrice = 123.45f;    // So will this

Other data types can be specified using suffixes. These are shown below:

uintU or u100U
longL or l100L
ulongUL or ul100UL
floatF or f123.45F
decimalM or m123.45M

This table does not include all of the C# numeric data types. The byte, sbyte, short and ushort data types do not have designated suffixes. However, integer literals can be assigned to variables of these types. They will be implicitly converted, assuming that the value is within the valid range.

Using Hexadecimal

Programmers often represent numbers using hexadecimal, rather than decimal. You identify hexadecimal values with the '0x' prefix. The two following lines of code are therefore equivalent:

int sixteen = 16;
int sixteen = 0x10;

Boolean Literals

Two literal values are permitted for the Boolean data type. These are false and true. No other values are valid.

bool grassIsGreen = true;
bool cowsGoBaa = false;
6 August 2006