C# Literals

Learn via video courses
Topics Covered

Overview

In programming, C# literals are fixed values (or hard-coded values) assigned to variables that cannot be changed during program execution. They represent various types of constants such as integers, floats, chars, and strings. Integer literals can be in decimal, binary, octal, or hexadecimal format. Floating-point literals represent numbers with fractional parts. Character literals represent individual characters, whereas string literals represent sequences. Boolean literals are 'true' or 'false', and 'null' represents absence. By providing direct, recognizable values for data initialization and manipulation, C# literals enhance code clarity and efficiency.

Example:

In this example, x is a variable that holds the value 100, which is a literal value. The assignment of a literal value to a variable allows you to store and manipulate data in your program.

Top 5 Types of Literals in C#

Integer Literals

An integer literal is a value of the integer type. It can be represented as an octal, decimal, binary, or hexadecimal constant. Decimal numbers don't need a prefix. Integer literals can also have suffixes like U or u for unsigned numbers, and l or L for long numbers. By default, all literals are treated as int type. We have several ways of representing literals when working with integral data types like as byte, short, int, and long:

  • Decimal Literals (Base 10): For decimal literals, they can include digits from 0 to 9, and no prefix is necessary.

  • Octal Literals (Base 8): The numbers from 0 to 7 are included in octal literals, and 0 is used as a prefix to specify the form of octal-type literals.

  • Hexa-decimal Literals (Base 16): Hexadecimal literals can consist of digits from 0 to 9 and characters from A to F, in both uppercase and lowercase forms. They are preceded by 0x or 0X. C# is a case-sensitive programming language, but it is not case-sensitive here.

  • Binary Literals (Base 2): Binary literals, which are expressed in base 2, only use the digits 1 and 0. To denote a binary number, it must be prefixed with "0b".

Floating-point Literals

A floating-point literal is a representation consisting of an integer component, a dot (decimal point), a fractional component, and an exponent component. These can be expressed in either standard decimal form or in exponential notation.

Example:

Character Literals

Character literals are enclosed within single quotes (''). There are three ways to represent character literals.

  • Single Quote: Character literals can be expressed as individual characters within single quotes.

  • Unicode Representation: Character literals can also be created using Unicode representation, which includes using the syntax uxxxx, where xxxx represents hexadecimal values.

  • Escape Sequence: Certain escape characters are recognized as character literals, allowing you to represent special characters using escape sequences.

    The following are various escape sequence literals and their meanings.

    Escape SequenceMeaning
    \\ character
    \’‘ character
    ?? character
    \”” character
    \bBackspace
    \aAlert or Bell
    \nNew Line
    \fForm Feed
    \rCarriage Return
    \vVertical Tab
    \xhhHexadecimal number

String Literals

String literals are enclosed in double quotations (""). They can also start with @"", making it simpler to handle large lines by breaking them into multiple lines with string literals and separating them with spaces.

Example:

Boolean Literals

Boolean literals are restricted to just two values: true and false.

Example:

Examples of C# Literals

The examples below demonstrate how to implement all of the literals in C#.

Example 1: Integer Literal

Output:

Explanation:
The above C# code demonstrates the use of multiple integer literals and their values. It uses variables to store these literals: a decimal literal (10), an octal literal (062, which is equivalent to 50 in decimal), a hexadecimal literal (0x132f0x132f, which is equivalent to 4911 in decimal), and a binary literal (0b101, which is equivalent to 5 in decimal). The code then prints these values using the Console.WriteLine statements.

Example 2: Floating Point Literal

Output:

Explanation: The above C# code initializes three double variables: a, b, and c. The value 632.281 is assigned to a decimal-point literal, while 0574.951, despite starting with a zero, is recognized as a decimal-point literal due to the presence of the decimal point. The value of c is expressed in exponential notation as 243183 multiplied by 10 to the power of -4 (0.0001), with the F suffix implying a float type, although it's important to note that it doesn't affect the default double-precision nature of these literals. Following that, the Console.WriteLine statements output the values of these variables.

Example 3: Character Literals

Output:

Explanation:

The above C# code initializes two character variables ch1 and ch2. The character literal 'a' is allocated to ch1, while the Unicode representation of 'q' is assigned to ch2 using the escape sequence u0071. The Console.WriteLine statements then output the values of ch1 and ch2. Additionally, the last Console.WriteLine statement prints the string "Hello" followed by a newline (\n) and then "World" followed by a tab (\t) and finally an exclamation mark, resulting in the formatted output "Hello\nWorld\t!".

Example 4: String Literal

Output:

Explanation:

The C# code above declares two string variables, str1 and str2. The string literal "Hello, world!" is assigned to str1, while the string literal @"This is a long string." is assigned to str2. The values of str1 and str2 are then printed by the Console.WriteLine statements, resulting in the display of the respective string literals.

Example 5: Boolean Type Literal

Output:

Explanation: The above C# code initializes two boolean variables, a and b, with the values true and false, respectively. The Console.WriteLine statements then output the values of a and b, resulting in the display of the corresponding boolean literals.

Conclusion

  • C# literals are fixed values (or hard-coded values) assigned to variables that cannot be changed during program execution.
  • In C#, literals represent constant values of various data types, such as integers, floating-point numbers, characters, strings, and more.
  • An integer literal is a value of the integer type. It can be represented as an octal, decimal, binary, or hexadecimal constant.
  • A floating-point literal is a representation consisting of an integer component, a dot (decimal point), a fractional component, and an exponent component.
  • Character literals are enclosed within single quotes ('').
  • String literals are enclosed in double quotations ("").
  • Boolean literals are restricted to just two values: true and false.