# C# - Literals

## Literals

A literal is a source code representation of a value.

## Boolean literals

There are two Boolean literal values: true and false.

boolean-literal:

• true
• false

The type of a boolean-literal is bool.

## 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 Literals can be specified as the number without any fractions.

An integer literal can be hexadecimal, octal or decimal.

To specify hexadecimal and octal integer literals we used prefix as 0x/0X and o/O respectively. No prefix is used for the decimal.

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.

These are some examples of Integer Literals:

```    95  // decimal
0xAB10  // hexadecimal
076  // octal
12u // unsigned int
32ul // unsigned long
45L // long
39 // int
```

## Real literals

Real literals are used to write values of types 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:

• 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. 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.

Here are some examples of the Floating Point Literals:

```    3.14f // float value
3.123456 // double value
314.e54 // exponent floating value
234.55m //decimal value
```

## Character literals

A character literal represents a single character, and usually consists of a character in quotes, as in 'a'.

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 simple escape sequence represents a Unicode character encoding, as described in the table below.

Escape Sequence Description
\a Alert (bell)
\b Backspace
\f Form feed
\n New line (linefeed)
\r Carriage return
\t Horizontal tab
\v Vertical tab
\0 Null
\' Single quote
\" Double quote
\\ Backslash

## 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.

## The null literal

The type of a null-literal is the null type.

Recent Posts
Popular Posts