C# Reference ยท Types and Literals
C# literals
Reference for C# literals: integer, floating-point, decimal, hex and binary forms, digit separators, suffixes, characters, escapes, verbatim and interpolated strings.
A literal is a value written directly in source code: 42, 3.14, 'x', "text", true, null. Each literal has a type that the compiler decides from its form, and suffixes or prefixes let you choose a different type or notation.
Knowing the default types matters, because they decide overload resolution, overflow behavior and precision. 1 / 2 is integer division because both literals are int; 0.1 is a double, not a decimal.
int a = 1_000_000; long b = 0xFF_FFL; double c = 6.02e23; decimal d = 19.99m; char e = '\u00E9'; string f = "tab\there";
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
| Literal | Examples | Type |
|---|---|---|
| Integer | 42, 0xFF, 0b101, 1_000 |
First of int, uint, long, ulong that fits |
| Integer suffixes | 10U, 10L, 10UL, 10LU |
uint, long, ulong (suffixes case-insensitive; prefer uppercase L) |
| Real | 1.5, 1e10, .5, 1.5D |
double |
| Real suffixes | 1.5F, 1.5M |
float, decimal |
| Boolean | true, false |
bool |
| Character | 'a', '\n', '\u0041', '\x41' |
char |
| String | "text", @"verbatim", $"{x}", """raw""" |
string |
| Null | null |
no type; converts to any reference or nullable type |
| Default | default |
the default value of the target type |
Escape sequences in ordinary strings and characters: \', \", \\, \0, \a, \b, \f, \n, \r, \t, \v, \uXXXX, \UXXXXXXXX (strings only), \xH to \xHHHH. Verbatim strings (@"...") take everything literally except "". Raw string literals are covered on their own page, and u8 literals on UTF-8 string literals.
There is no s, d or b suffix for short, sbyte or byte; a constant integer expression converts implicitly when it fits the target range.
Errors. A literal outside the range of its target is a compile error (byte b = 300;), and the compiler rejects an integer literal that is too large for ulong. A real literal that overflows its type is also an error, not infinity.
Usage notes
- Use digit separators (
1_000_000,0xFF_EC,0b0001_0010) for readability; they may appear between digits but not at the start, end or next to the decimal point or prefix. doubleis the default for real literals.float f = 1.5;does not compile; write1.5f. Money should bedecimalwithm:19.99m.- Integer division.
5 / 2is2. Make one side real (5 / 2.0) when you want a fraction. - Hex literals can change sign.
0xFFFFFFFFis auint, whileunchecked((int)0xFFFFFFFF)is-1. Useuint/ulongfor bit masks. - Character arithmetic yields
int('A' + 1); cast back tochar. - String concatenation goes left to right, so
1 + 2 + "abc"is"3abc"while"abc" + 1 + 2is"abc12". - UTF-16. A
charholds a single UTF-16 code unit; characters outside the basic plane need two (a surrogate pair) and cannot be a singlecharliteral. - Constant folding. Literal expressions and
constvalues are computed by the compiler, which is why a constant likeint.MaxValue + 1is a compile error (useunchecked) while the same code on variables overflows at run time. - Null literal.
nullcannot be used withvaralone; anddefaultis a literal for the "zero" value.