You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: standard/lexical-structure.md
+95-22Lines changed: 95 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -489,34 +489,75 @@ The type of a *boolean_literal* is `bool`.
489
489
490
490
#### 7.4.5.3 Integer literals
491
491
492
-
Integer literals are used to write values of types `int`, `uint`, `long`, and `ulong`. Integer literals have two possible forms: decimaland hexadecimal.
492
+
Integer literals are used to write values of types `int`, `uint`, `long`, and `ulong`. Integer literals have three possible forms: decimal, hexadecimal, and binary.
The type of an integer literal is determined as follows:
@@ -528,32 +569,51 @@ The type of an integer literal is determined as follows:
528
569
529
570
If the value represented by an integer literal is outside the range of the `ulong` type, a compile-time error occurs.
530
571
531
-
> *Note*: As a matter of style, it is suggested that "`L`" be used instead of"`l`" when writing literals of type `long`, since it is easy to confuse the letter"`l`" with the digit"`1`". *end note*
572
+
> *Note*: As a matter of style, it is suggested that "`L`" be used instead of"`l`" when writing literals of type `long`, since it is easy to confuse the letter"`l`" with the digit"`1`". *end note*
532
573
533
574
To permit the smallest possible `int` and `long` values to be written as integer literals, the following two rules exist:
534
575
535
-
- When an *integer_literal* representing the value `2147483648` (2³¹) and no *integer_type_suffix* appears as the token immediately following a unary minus operator token ([§12.8.3](expressions.md#1283-unary-minus-operator)), the result (of both tokens) is a constant of type int with the value `−2147483648` (−2³¹</sup>). In all other situations, such an *integer_literal* is of type `uint`.
536
-
- When an *integer_literal* representing the value `9223372036854775808` (2⁶³) and no *integer_type_suffix* or the *integer_type_suffix*`L` or `l` appears as the token immediately following a unary minus operator token ([§12.8.3](expressions.md#1283-unary-minus-operator)), the result (of both tokens) is a constant of type `long` with the value `−9223372036854775808` (−2⁶³). In all other situations, such an *integer_literal* is of type `ulong`.
576
+
- When an *integer_literal* representing the value `2147483648` (2³¹) and no *integer_type_suffix* appears as the token immediately following a unary minus operator token ([§12.8.3](expressions.md#1283-unary-minus-operator)), the result (of both tokens) is a constant of type `int` with the value `−2147483648` (−2³¹). In all other situations, such an *integer_literal* is of type `uint`.
577
+
- When an *integer_literal* representing the value `9223372036854775808` (2⁶³) and no *integer_type_suffix* or the *integer_type_suffix*`L` or `l` appears as the token immediately following a unary minus operator token), the result (of both tokens) is a constant of type `long` with the value `−9223372036854775808` (−2⁶³). In all other situations, such an *integer_literal* is of type `ulong`.
578
+
579
+
> *Example*:
580
+
> ```csharp
581
+
>123// decimal, int
582
+
>10_543_765Lu// decimal, ulong
583
+
>1__234__567// decimal, int
584
+
>
585
+
>0xFf// hex, int
586
+
>0X1b_a0_44_fEL// hex, long
587
+
>0x1ade_3FE1_29AaUL// hex, ulong
588
+
>0xabc_// invalid; no trailing _ allowed
589
+
>
590
+
>0b101// binary, int
591
+
>0B1001_1010u// binary, uint
592
+
>0b1111_1111_0000UL// binary, ulong
593
+
>0B__111// OK; leading _ allowed
594
+
> ```
595
+
>*endexample*
537
596
538
597
#### 7.4.5.4 Real literals
539
598
540
599
Realliteralsareusedtowritevaluesoftypes `float`, `double`, and `decimal`.
If no *real_type_suffix* is specified, the type of the *real_literal* is `double`. Otherwise, the *real_type_suffix* determines the type of the real literal, as follows:
565
625
566
-
- A real literal suffixed by`F` or`f` is of type `float`.
567
-
> *Example*: The literals`1f, 1.5f, 1e10f`, and `123.456F` are all of type `float`. *end example*
568
-
- A real literal suffixed by`D` or`d` is of type `double`.
569
-
> *Example*: The literals`1d, 1.5d, 1e10d`, and `123.456D` are all of type `double`. *end example*
570
-
- A real literal suffixed by`M` or`m` is of type `decimal`.
571
-
> *Example*: The literals`1m, 1.5m, 1e10m`, and `123.456M` are all of type `decimal`. *end example*
572
-
This literal is converted to a `decimal` valueby taking the exact value, and, if necessary, rounding to the nearest representable value using banker's rounding ([§9.3.8](types.md#938-the-decimal-type)). Any scale apparent in the literal is preserved unless the value is rounded.
573
-
> *Note*: Hence, the literal `2.900m` will be parsed to form the `decimal` with sign`0`, coefficient`2900`, and scale`3`. *end note*
626
+
- A real literal suffixed by`F` or`f` is of type `float`.
627
+
> *Example*: The literals`1f, 1.5f, 1e10f`, and `123.456F` are all of type `float`. *end example*
628
+
- A real literal suffixed by`D` or`d` is of type `double`.
629
+
> *Example*: The literals`1d, 1.5d, 1e10d`, and `123.456D` are all of type `double`. *end example*
630
+
- A real literal suffixed by`M` or`m` is of type `decimal`.
631
+
> *Example*: The literals`1m, 1.5m, 1e10m`, and `123.456M` are all of type `decimal`. *end example*
632
+
This literal is converted to a `decimal` valueby taking the exact value, and, if necessary, rounding to the nearest representable value using banker's rounding ([§9.3.8](types.md#938-the-decimal-type)). Any scale apparent in the literal is preserved unless the value is rounded.
633
+
> *Note*: Hence, the literal `2.900m` will be parsed to form the `decimal` with sign`0`, coefficient`2900`, and scale`3`. *end note*
574
634
575
635
If the magnitude of the specified literal is too large to be represented in the indicated type, a compile-time error occurs.
576
636
@@ -580,6 +640,19 @@ The value of a real literal of type `float` or `double` is determined by using t
580
640
581
641
> *Note*: In a real literal, decimal digits are always required after the decimal point. For example, `1.3F` is a real literal but `1.F` is not. *end note*
582
642
643
+
> *Example*:
644
+
> ```csharp
645
+
>1.234_567// double
646
+
>.3e5f// float
647
+
>2_345E-2_0// double
648
+
>15D// double
649
+
>19.73M// decimal
650
+
>1.F// invalid; ill-formed (parsed as "1." and "F")
651
+
>1.234_// invalid; no trailing _ allowed in fraction
652
+
>.3e5_F// invalid; no trailing _ allowed in exponent
0 commit comments