C# Literals

C# Literals are value constants assigned to variables in a program. In C#, there are six types of literals are exist.

Integer Literals:

An Integer literal refers to a sequence of digits. There are two types of integers namely:

  • Decimal Integers
  • Hexadecimal Integers

Decimal Integers:

Decimal Integers consist of a set of digits, 0 to 9 preceded by an optional minus sign.
Example:

123, -321, 654321

 

Hexadecimal Integers:

Hexadecimal Integers refer to a sequence of digits preceded by 0x or 0X is considered as a Hexadecimal Integers. It may also include alphabets A, B, C, D, E, F or a, b, c, d, e, f.

  • A or a represents the number 10
  • B or b represents the number 11
  • C or c represents the number 12
  • D or d represents the number 13
  • E or e represents the number 14
  • F or f represents the number 15

 

Example:

0x, 0X2, 0X9F, 0Xbcd

 

Real Literals:

It is represented by numbers containing fractional parts that are called Real Literals.

Example:
0.50, 3.14, -0.15

A real literal may also be expressed in exponential notation.

Example:
8.16e47, 0.5e+4, -1.5e-1

Boolean Literals:

There are two types of Boolean literals and that are:

  • True
  • False

Single Character Literals:

A single character contains a single character enclosed within a pair of single quotation marks.
Example:
‘5’ ‘X’ ‘;”’

Where the character constant [no-highlight]’5′[/no-highlight] isn’t the same as the number 5. The last constant in the [no-highlight]”[/no-highlight] is a blank space.

String Literals:

A string literal is a sequence of characters enclosed between double quotes. The characters may be alphabets, digits, special characters and blank spaces.

Example:
“Hello” “Well Done” “X”

Backslash Character Literals:

In C#, there are supports for some special backslash character constants that are used in output methods.

SymbolDescription
'\a'Alert
'\b'Back Space
'\f'Form Feed
'\n'New Line
'\r'Carriage Return
'\t'Horizontal Tab
'\v'Vertical Tab
'\''Single Quote
'\"'Double Quote
'\\'Backslash
'\0'Null