Literals
A literal is a source code representation of a value. literal: 2.4.4.1 Boolean literals There are two boolean literal values: true and false. boolean-literal: The type of a boolean-literal is bool. 2.4.4.2 Integer literals Integer literals are used to write values of types int, uint, long, and ulong. Integer literals have two possible forms: decimal and hexadecimal. integer-literal: decimal-integer-literal: decimal-digits: decimal-digit: one of integer-type-suffix: one of hexadecimal-integer-literal: hex-digits: hex-digit: one of The type of an integer literal is determined as follows: · If the literal has no suffix, it has the first of these types in which its value can be represented: int, uint, long, ulong. · If the literal is suffixed by U or u, it has the first of these types in which its value can be represented: uint, ulong. · If the literal is suffixed by L or l, it has the first of these types in which its value can be represented: long, ulong. · If the literal is suffixed by UL, Ul, uL, ul, LU, Lu, lU, or lu, it is of type ulong. If the value represented by an integer literal is outside the range of the ulong type, a compile-time error occurs. 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”. To permit the smallest possible int and long values to be written as decimal integer literals, the following two rules exist: · When a decimal-integer-literal with the value 2147483648 (231) and no integer-type-suffix appears as the token immediately following a unary minus operator token (§7.7.2), the result is a constant of type int with the value −2147483648 (−231). In all other situations, such a decimal-integer-literal is of type uint. · When a decimal-integer-literal with the value 9223372036854775808 (263) and no integer-type-suffix or the integer-type-suffix L or l appears as the token immediately following a unary minus operator token (§7.7.2), the result is a constant of type long with the value −9223372036854775808 (−263). In all other situations, such a decimal-integer-literal is of type ulong. 2.4.4.3 Real literals Real literals are used to write values of types float, double, and decimal. real-literal: exponent-part: sign: one of real-type-suffix: one of 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: · A real literal suffixed by F or f is of type float. For example, the literals 1f, 1.5f, 1e10f, and 123.456F are all of type float. · A real literal suffixed by D or d is of type double. For example, the literals 1d, 1.5d, 1e10d, and 123.456D are all of type double. · A real literal suffixed by M or m is of type decimal. For example, the literals 1m, 1.5m, 1e10m, and 123.456M are all of type decimal. This literal is converted to a decimal value by taking the exact value, and, if necessary, rounding to the nearest representable value using banker's rounding (§4.1.7). Any scale apparent in the literal is preserved unless the value is rounded or the value is zero (in which latter case the sign and scale will be 0). Hence, the literal 2.900m will be parsed to form the decimal with sign 0, coefficient 2900, and scale 3. If the specified literal cannot be represented in the indicated type, a compile-time error occurs. The value of a real literal of type float or double is determined by using the IEEE “round to nearest” mode. Note that 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. 2.4.4.4 Character literals A character literal represents a single character, and usually consists of a character in quotes, as in 'a'. character-literal: character: single-character: simple-escape-sequence: one of hexadecimal-escape-sequence: A character that follows a backslash character (\) in a character must be one of the following characters: ', ", \, 0, a, b, f, n, r, t, u, U, x, v. Otherwise, a compile-time error occurs. A hexadecimal escape sequence represents a single Unicode character, with the value formed by the hexadecimal number following “\x”. If the value represented by a character literal is greater than U+FFFF, a compile-time error occurs. A Unicode character escape sequence (§2.4.1) in a character literal must be in the range U+0000 to U+FFFF. A simple escape sequence represents a Unicode character encoding, as described in the table below.
The type of a character-literal is char. 2.4.4.5 String literals C# supports two forms of string literals: regular string literals and verbatim string literals. A regular string literal consists of zero or more characters enclosed in double quotes, as in "hello", and may include both simple escape sequences (such as \t for the tab character), and hexadecimal and Unicode escape sequences. A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences, and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines. string-literal: regular-string-literal: regular-string-literal-characters: regular-string-literal-character: single-regular-string-literal-character: verbatim-string-literal: verbatim-string-literal-characters: verbatim-string-literal-character: single-verbatim-string-literal-character: quote-escape-sequence: A character that follows a backslash character (\) in a regular-string-literal-character must be one of the following characters: ', ", \, 0, a, b, f, n, r, t, u, U, x, v. Otherwise, a compile-time error occurs. The example string a = "hello, world"; // hello, world string c = "hello \t world"; // hello world string e = "Joe said \"Hello\" to me"; // Joe said "Hello" to me string g = "\\\\server\\share\\file.txt"; // \\server\share\file.txt string i = "one\r\ntwo\r\nthree"; shows a variety of string literals. The last string literal, j, is a verbatim string literal that spans multiple lines. The characters between the quotation marks, including white space such as new line characters, are preserved verbatim. Since a hexadecimal escape sequence can have a variable number of hex digits, the string literal "\x123" contains a single character with hex value 123. To create a string containing the character with hex value 12 followed by the character 3, one could write "\x00123" or "\x12" + "3" instead. The type of a string-literal is string. Each string literal does not necessarily result in a new string instance. When two or more string literals that are equivalent according to the string equality operator (§7.10.7) appear in the same program, these string literals refer to the same string instance. For instance, the output produced by class Test is True because the two literals refer to the same string instance. 2.4.4.6 The null literal null-literal: The null-literal can be implicitly converted to a reference type or nullable type.
|