Literals Versus Variables
In the examples you've looked at so far, you have seen a lot of numbers and values used that were not variables. Often, you will want to type a number or value into your source code. A literal value stands on its own within the source code. For example, in the following lines of code, the number 10 and the value "Bob is a fish" are literal values. As you can see, literal values can be put into variables.
int x = 10; myStringValue = "Bob is a fish";
Numeric Literals
In many of the examples, you have used numeric literals. By default, a numeric literal is either an int or a double. It is an int if it is a whole number, and it is a double if it is a floating-point number. For example, consider the following:
nbr = 100;
In this example, 100 is a numeric literal. By default, it is considered to be of type int, regardless of what data type the nbr variable is. Now consider the following:
nbr = 99.9;
In this example, 99.9 is also a numeric literal; however, it is of type double by default. Again, this is regardless of the data type that nbr is. This is true even though 99.9 could be stored in a type float. In the following line of code, is 100. an int or a double?
x = 100.;
This is a tough one. If you guessed int, you are wrong. Because there is a decimal included with the 100, it is a double.
Integer Literal Defaults
When you use an integer value, it is put into an int, uint, long, or ulong depending on its size. If it will fit in an int or uint, it will be. If not, it will be put into a long or ulong. If you want to specify the data type of the literal, you can use a suffix on the literal. For example, to use the number 10 as a literal long value(signed or unsigned), you write it like the following:
10L;
You can make an unsigned value by using a u or a U. If you want an unsigned literal long value, you can combine the two suffixes: ul.
NOTE
The Microsoft C# compiler gives you a warning if you use a lowercase l to declare a long value literal. The compiler provides this warning to help you be aware that it is easy to mistake a lowercase l with the number 1.
Floating-Point Literal Defaults
As stated earlier, by default, a decimal value literal is a double. To declare a literal that is of type float, you include an f or F after the number. For example, to assign the number 4.4 to a float variable, my_float, you use the following:
my_float = 4.4f;
To declare a literal of type decimal, you use a suffix of m or M. For example, the following line declares my_decimal to be equal to the decimal number 1.32.
my_decimal = 1.32m;
Boolean Literals (true and false)
Boolean literals have already been covered. The values true and false are literal. They also happen to be keywords.
String Literals
When you put characters together, they make words, phrases, and sentences. In programming parlance, a group of characters is called a string. A string can be identified because it is contained between a set of double quotes. You have actually been using strings in the examples so far. For example, the Console.WriteLine routine uses a string. A string literal is any set of characters between double quotes. The following are examples of strings:
"Hello, World!" "My Name is Bradley" "1234567890"
Because these numbers are between quotation marks, the last example is treated as a string literal rather than as a numeric literal.
NOTE
You can use any of the special characters from Table 3.3 inside a string.