Relational and Equality Comparison Operators
Increment and Decrement Operators
This chapter defines expressions and describes the operators provided by C#. It also explains how you can define the C# operators to work with your user-defined classes.
An expression is a string of operators and operands. The following are some of the constructs that can act as operands:
The C# operators take one, two, or three operands. An operator does the following:
Expressions can be combined, using operators, to create other expressions, as shown in this expression, with three operators and four operands:
Evaluating an expression is the process of applying each operator to its operands, in the proper sequence, to produce a value.
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 difference between the double
literal and the float
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:
bool
has two literals: true
and false
.null
means that the variable is not set to a reference in memory.Integer literals are the most commonly used literals. They are written as a sequence of decimal digits, with the following:
For example, the following lines show four literals for the integer 236. Each is interpreted by the compiler as a different type of integer, depending on its suffix.
236 // int
236L // long
236U // unsigned
236UL // unsigned long
Integer type literals can also be written in hexadecimal (hex) form. The digits must be the hex digits (0 through F), and the string must be prefaced with either 0x
or 0X
(numeral 0, letter x).
Figure 8-1 shows the forms of the integer literal formats. Components with names in square brackets are optional.
Figure 8-1. The integer literal formats
Table 8-1 lists the integer literal suffixes. For a given suffix, the compiler will interpret the string of digits as the smallest of the corresponding integer types that can represent the value without losing data.
For example, take the literals 236
and 5000000000
, neither of which has a suffix. Since 236
can be represented with 32 bits, it will be interpreted by the compiler as an int
. The larger number, however, won’t fit into 32 bits, so the compiler will represent it as a long
.
Table 8-1. Integer Literal Suffixes
Suffix | Integer Type | Notes |
None | int , uint , long , ulong |
|
U , u |
uint , ulong |
|
L , l |
long , ulong |
Using the lowercase letter l is not recommended, because it is easily mistaken for the digit 1. |
ul , uL , Ul , UL lu , Lu , lU , LU |
ulong |
Using the lowercase letter l is not recommended, because it is easily mistaken for the digit 1 |
Literals for real numbers consist of the following:
For example, the following code shows various formats of literals of the real types:
float f1 = 236F;
double d1 = 236.714;
double d2 = .35192;
double d3 = 6.338e-26;
Figure 8-2 shows the valid formats for real literals. Components with names in square brackets are optional. Table 8-2 shows the real suffixes and their meanings.
Figure 8-2. The real literal formats
Table 8-2. Suffixes for the Real Literals
Suffix | Real Type |
None | double |
F , f |
float |
D , d |
double |
M , m |
decimal |
A character literal consists of a character representation between two single quote marks. A character representation can be any of the following: a single character, a simple escape sequence, a hex escape sequence, or a Unicode escape sequence.
char
.For example, the following code shows various formats of character literals:
char c1 = 'd'; // Single character
char c2 = '
'; // Simple escape sequence
char c3 = 'x0061'; // Hex escape sequence
char c4 = 'u005a'; // Unicode escape sequence
Table 8-3 shows some of the important special characters and their encodings.
Table 8-3. Important Special Characters
Name | Escape Sequence | Hex Encoding |
Null |
|