Information Technology Reference
In-Depth Information
Multiple Markers and Values
In C#, you can use any number of markers and any number of values.
The values can be used in any order.
The values can be substituted any number of times in the format string.
For example, the following statement uses three markers and only two values. Notice that
value
1
is used before value
0
, and that value
1
is used twice.
Console.WriteLine("Three integers are {1}, {0} and {1}.", 3, 6);
This code displays the following on the screen:
Three integers are 6, 3 and 6.
A marker must not attempt to reference a value at a position beyond the length of the list
of values. If it does, however, it will not produce a compile error, but a runtime error (called an
exception).
For example, in the following statement there are two substitution values, with positions
0
and
1
. The second marker, however, references position
2
—which does not exist. This will pro-
duce a runtime error.
Position 0
Position 1
↓
↓
Console.WriteLine("Two integers are {0} and {2}.", 3, 6); // Error!
↑
There is no position 2 value.