Information Technology Reference
In-Depth Information
Literals
Literals
are numbers or strings typed into the source code that represent a specific, set value of
a specific type.
For example, the following code shows literals of six types. Notice, for example, the differ-
ence between the
double
literal and the
float
literal.
static void Main()
Literals
{
↓
Console.WriteLine("{0}", 1024); // int literal
Console.WriteLine("{0}", 3.1416); // double literal
Console.WriteLine("{0}", 3.1416F); // float literal
Console.WriteLine("{0}", true); // boolean literal
Console.WriteLine("{0}", 'x'); // character literal
Console.WriteLine("{0}", "Hi there"); // string literal
}
The output of this code is the following:
1024
3.1416
3.1416
True
x
Hi there
Because literals are written into the source code, their values must be known at
compile time.
Several of the predefined types have their own forms of literal:
Type
bool
has two literals:
true
and
false
.
For reference type variables, literal
null
means that the variable is not set to a reference
in memory.