Literals and Constants in C# Programming


In any programing language including C#, constants are values which are fixed and cannot be changed anytime during program execution. They can be of any data type. We can use const to make a variable constant. Example of some constants are:

const float pi = 3.14159;
const char ch = 'character';
int i = 5;

In above examples, pand ch are constant and their value cannot be changed during execution. But, i is not a constant.


Literals are the values itself. Let us look at above example to know literals better. In above example, 3.14159, ' character'  and are literals. Type of literals in C# are:

  1. Boolean Literal
  2. Integer Literal
  3. Real Literal
  4. Character Literal
  5. String Literal
  6. Null Literal

1. Boolean Literal

Boolean Literal can store two values: true and falsebool data type is used to store these values. Example of boolean literal is shown below:

bool condition = true;

2. Integer Literal

Integer Literal can store numeric value. They can be decimal, octal or hexadecimal type. They can also have a sign, prefix or suffix.

  • We can use + or - to denote sign of the integer.
  • We can use prefix to denote number format of the integer. We use '0x' or '0X' to denote hexadecimal number, '0' to denote octal number and if we don;t use any prefix it is decimal by default.
  • We can use suffix 'u' - 'U' or 'l' - 'L' to denote type on integer, 'l' or 'L' for long and 'u' or 'U' for unsigned type. If no suffix is used it is int by default.

Examples of integer literals are:

55 //decimal
0x125f //hexadecimal
056 //octal
10 //integer
10u //uint
10l //long
10ul //ulong

​3. Real Literal

Real Literal stores numeric value. They are of floating number type. They can also have a sign, suffix or decimal point. They can also be in exponential format.

  • We can use + or - to denote sign of the floating number.
  • We can use suffix 'f' - 'F' or 'd' - 'D' or 'm' - 'M' to denote type on real literal, 'f' or 'F' for float, 'd' or 'D' for double and 'm' or 'M' for decimal typeIf no suffix is used it is double by default.
  • 'e' can be used for exponential types.

Examples of real literals are:

1.23 //double
1.23f //float
1.23d //double
1.23m //decimal
e23 //exponent. Means 1023

4. Character Literal

Character Literals are single unicode character. They are enclosed between single quotes. Values that can be stored in character literals are character (example: 'a'), character code (example: '\u0097') and escaping sequence. It is denoted by char data type.

Escaping sequence have special meaning and it cannot be used directly. List of some escaping sequence is given below:

  • \\ - used to get '\' character
  • \' - used to get single quote
  • \" - used to get double quote
  • \n - newline
  • \r - carriage return
  • \b - backspace
  • \t - horizontal tab
  • \a - alert
  • \f - form feed
  • \v - vertical tab
  • \0 - null
  • \uXXXX - character corresponding to its unicode number

5. String Literal

String Literals are set of characters enclosed between double quotes "" or @"". They can store characters or escape sequences. Strings initialized using @"" are known as verbatim string. Escape sequences does not work in verbatim string. Lines can be broke down to small lines by just using blank space. Example of string literals are given below:

"string literal" //Output: string literal
@"string literal" //Output: string literal
"string \t literal" //Output: string      literal
"string //string 
literal" //literal
""Hi"" //"Hi"

6. Null Literal

Null Literal is literal that denotes null type. We can use it to denote that nothing is referenced to the null constant or variable. Example of null literal is given below:

int a = null;
if (a == null)
     Console.WriteLine("null value");
null value*/