Literals are fixed values that are assigned to variables and cannot be changed during the execution of the program. They are also known as constants or hard-coded values. C# Literals can be of different types, such as integer, floating-point, character, string, boolean, and null.
In this blog post, we will explore each type of C# literals and learn how to use them in our code.
Types of C# Literals
## Integer C# Literals
Integer literals are used to represent values of integral types, such as int, uint, long, ulong, etc. They can be written in decimal, binary, hexadecimal, or octal notation. The default type of an integer literal is int, unless it is suffixed with a letter to indicate a different type. For example:
- 42 is an int literal
- 42U is a uint literal
- 42L is a long literal
- 42UL is a ulong literal
To write an integer literal in binary notation, we must prefix it with 0b or 0B. For example:
- 0b101010 is a binary literal equivalent to 42
- 0B101010U is a binary uint literal equivalent to 42U
To write an integer literal in hexadecimal notation, we must prefix it with 0x or 0X. For example:
- 0x2A is a hexadecimal literal equivalent to 42
- 0X2AU is a hexadecimal uint literal equivalent to 42U
To write an integer literal in octal notation, we need to prefix it with 0. For example:
- 052 is an octal literal equivalent to 42
- 052U is an octal uint literal equivalent to 42U
Note that octal literals are not supported by C# versions prior to C# 7.0.
## Floating-point C# Literals
Floating-point literals are used to represent values of floating-point types, such as float, double, or decimal. They consist of an integer part, a decimal point, a fractional part, and an optional exponent part. The default type of a floating-point literal is double, unless it is suffixed with a letter to indicate a different type.
For example:
- 3.14 is a double literal
- 3.14F or 3.14f is a float literal
- 3.14M or 3.14m is a decimal literal
To write a floating-point literal in exponential notation, we need to use E or e followed by an exponent.
For example:
- 3.14E2 is a double literal equivalent to 314
- 3.14E-2 is a double literal equivalent to 0.0314
- 3.14E2F or 3.14e2f is a float literal equivalent to 314F
- 3.14E2M or 3.14e2m is a decimal literal equivalent to 314M
## Character C# Literals
Character literals are used to represent single characters of type char. They are enclosed in single quotes and can be either plain characters or escape sequences.
For example:
- ‘A’ is a char literal representing the letter A
- ‘\n’ is a char literal representing the newline character
- ‘\” is a char literal representing the single quote character
- ‘\u0041’ is a char literal representing the Unicode character U+0041 (A)
Escape sequences are special characters that start with a backslash () and have a predefined meaning. They are used to represent characters that are not printable or have special functions.
Some common escape sequences are:
- \n for newline
- \t for tab
- \r for carriage return
- \b for backspace
- \a for alert (beep)
- \ for backslash
- \’ for single quote
- \” for double quote
- \u followed by four hexadecimal digits for Unicode character
- \x followed by one or more hexadecimal digits for Unicode character
## String C# Literals
String literals are used to represent sequences of characters of type string. They are enclosed in double quotes and can be either regular strings or verbatim strings.
For example:
- “Hello” is a regular string literal representing the word Hello
- @”Hello” is a verbatim string literal representing the word Hello
Regular strings can contain plain characters or escape sequences, as described in the previous section.
For example:
- “Hello\nWorld” is a regular string literal that contains a newline escape sequence
- “C:\Windows\System32” is a regular string literal that contains backslash escape sequences
Verbatim strings can contain plain characters or double quotes, but not escape sequences. They are prefixed with @ and can span multiple lines. They are useful for writing strings that contain backslashes, such as file paths or regular expressions, without having to escape them.
For example:
- @”Hello World” is a verbatim string literal that contains a newline character
- @”C:\Windows\System32″ is a verbatim string literal that contains backslashes
To write a double quote in a verbatim string, we need to use two consecutive double quotes.
For example:
- @”””Hello”” World” is a verbatim string literal that contains double quotes
## Null C# Literals
Null literals are used to represent the absence of a value. They are written as null and can be assigned to variables of reference types or nullable value types.
For example:
- string s = null; // s is a reference type variable that has no value
- int? i = null; // i is a nullable value type variable that has no value
Null literals cannot be assigned to variables of non-nullable value types, such as int, double, or bool.
For example:
- int x = null; // compile-time error
- bool b = null; // compile-time error
## Boolean C# Literals
Boolean literals are used to represent values of type bool, which can be either true or false. They are written as true or false and can be assigned to variables of type bool or used in conditional expressions.
For example:
- bool flag = true; // flag is a bool variable that has the value true
- if (flag) // conditional expression that evaluates to true
{
Console.WriteLine(“Flag is true”);
}
## Raw String C# Literals
Raw string literals are a new feature introduced in C# 11 that allow us to write strings that are multi-line, or use any characters requiring escape sequences, without having to escape them. Raw string literals remove the need to ever use escape sequences. We can write the string, including whitespace formatting, how we want it to appear in output.
A raw string literal starts and ends with a sequence of at least three double quote characters (“””). We can use more than three consecutive characters to start and end the raw string literal, as long as they match in number. The content of the raw string literal is everything between the starting and ending delimiters, excluding the leading and trailing newlines.
To write a double quote in a raw string literal, we need to use two consecutive double quotes. To write the same number of consecutive double quotes as the starting and ending delimiters, we need to use one more double quote.
For example:
- “””Hello World””” is a raw string literal that contains the text Hello World
- “”””Hello”” World”””” is a raw string literal that contains the text “Hello” World
- “””””Hello”””” World””””” is a raw string literal that contains the text “””Hello””” World
Raw string literals are useful for writing strings that contain special characters, such as JSON, XML, HTML, regex, etc., without having to escape them.
For example:
var json = """{ "name": "John", "age": 25, "hobbies": ["reading", "coding", "gaming"] }"""; //
json is a raw string literal that contains valid JSON data
C# Literals: Conclusion
In this blog post, we have learned about different types of C# literals and how to use them in our code. Literals are fixed values that are assigned to variables and cannot be changed during the execution of the program. They can be of various types, such as integer, floating-point, character, string, null, boolean, and raw string literals. Each type of literal has its own syntax and rules for writing and using them. We have seen some examples of each type of literal and their common use cases.
We hope you have enjoyed this blog post and learned something new about C# literals. If you have any questions or feedback, please feel free to leave a comment below.