 .NET 1.1C# 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 simply a fixed number. This number may be assigned to a variable or used in a calculation. Literals are sometimes also known as constants. However, I will avoid using this term further as there are other types of constants available in the C# language. Here are a few examples of literals being assigned to variables:
int facesOnADie = 6; // 10 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 some strange errors to appear when compiling your application. For example, if we take the literal value, 123.45, this is of the double data type. If you try to assign this literal value to a variable defined as a float, the compilation of the program will fail.
float unitPrice = 123.45; // This will not compile
The problem here is that you cannot always implicitly convert a double (123.45) into a float. The C# compiler understands this and stops the code from being compiled. To avoid this problem, it is important to understand how C# determines the data type of a numeric literal.
How C# Allocates Numeric Literal Data Types
C# employs a few simple rules to determine the type of a numeric literal within code. Firstly, the numeric literal is checked to see if it is an integer or if it contains a decimal point. For integers, the smallest possible data type for the size of the literal is selected. This does not include short or ushort so an integer literal will be of type int, uint, long or ulong.
Where a literal includes a decimal point, the data type is always assumed to be double. This data type provides the largest range of possible floating-point values; though it does not provide the greatest accuracy.
Overriding Numeric Literal Data Types
The rules that C# uses to determine the data type for a literal are satisfactory in most 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 literal value to indicate to the compiler that a different type should be used. For example, when allocating a literal to a float variable, the 'F' or 'f' suffix is required.
float unitPrice = 123.45; // This will not compile
float unitPrice = 123.45F; // This will compile
float unitPrice = 123.45f; // So will this
There are other data types that can be specified using suffixes. These are listed in the following table:
| Type | Suffix | Example |
|---|
| uint | U or u | 100U | | long | L or l | 100L | | ulong | UL or ul | 100UL | | float | F or f | 123.45F | | decimal | M or m | 123.45M |
This table does not include all of the numeric data types of C#. The byte, sbyte, short and ushort data types are omitted and do not have designated suffixes. However, integer literal can be assigned to variables of these data types and will be implicitly converted, assuming that the value is appropriate for storage in the variable.
Using Hexadecimal
As a programmer, it is often useful to represent numbers using the hexadecimal number base, rather than decimal. C# allows the use of hexadecimal numbers with the use of the '0x' prefix. The two following lines of code are functionally equivalent:
int sixteen = 16;
int sixteen = 0x10;
Boolean Literals
There are two literals that are defined as using the Boolean data type. These are false and true. These two values are the only literals that may be assigned to a variable of type bool.
bool grassIsGreen = true;
bool cowsGoBaa = false;
|