[edit] C# Literals
A literal identifier specifies the type a value should be. This is done by placing a letter or sequence of characters at the end of the literal. Take a look at the following table:
C# Literals
|
Types |
Suffix |
Use |
|
uint |
u,U |
5U |
|
ulong |
ul,UL |
15U |
|
long |
l,L |
1000L |
|
float |
f,F |
0.5F |
|
double |
d, D |
1.22D |
|
decimal |
m,M |
0.9M |
In C# there are many decimal point literals (float, decimal, double) so how will the compiler know which value to choose. For some reason when you don't specify what kind of decimal point number the variable is the compiler automatically chooses a double. For example:
float a = 0.9 //This is not acceptable and will produce errors
The compiler thinks that 0.9 is a type double and cannot convert it to a float. To make the compiler convert 0.9 to a float put a 'f' and the end of 0.9. All declaration must have a literal except integer and double values. Take a look at the code below.
using System; class Program { static void Main(string[] args) { float a = 0.9f; Console.WriteLine(a); Console.Read(); } }
This code will compile fine with no errors. The chart above notes all the types that require a character literal.
|