Since variables hold specific values ​​and serve as the name of memory locations, they can be changed multiple times during a program run. But constants are opposite because these values ​​do not change while running the program; you cannot change any values. These fixed values are technically termed as literals. In this chapter, you will learn about the constants available in C#.

What Are Constants(Literals) in C#?

Constants can be termed as those immutable values known at compile-time, and values cannot be changed during the program's life. In other words, constants are used for declaring any C# identifier whose values remain the same during the entire program run.

Types of C# Constants

There are four significant types of literals supported by C#. These are:
  • Character Literals
  • Integer Literals
  • Floating-point Literals
  • String Literals

Let us discuss each of them in brief.

Character Literals

Character literals are those literals where the data is enclosed within a pair of single quotes. These literals are plain characters that are available in the ASCII table. These can be of three types:

  • Plain characters (example: 'g', 'd', 's', '6', ' ' etc)
  • Escape sequence (example: '\n', '\t', '\a', '\?'etc)
  • Universal characters (example: '\u02C0')

The escape sequence is explained separately in further lessons.

Integer Literals

These types of literals can be a decimal value or even hexadecimal constant, identified by the compiler using their prefix, which is termed as the base or radix of a value. For hexadecimal value, the prefix is 0x or 0X, and no prefix is required for decimal values (which is the literal default type). Also, 'U' or 'u' and 'L' or 'l' are used as a suffix for determining unsigned and long values.
Example: 0xFeeL, 0x4b, 30u, 50l, 642 etc.

Floating-point Literals

These types of literals have two parts - first, the integral part in the left of the decimal point and the fractional part in the right of the decimal. These types of literals can be expressed in two forms:

  1. Decimal format (you have to use the decimal point for representing).
  2. Exponential format (you have to use the 'E' or 'e' for representing).
    Example: 213457E-2F, 8.34467, .e57, 842f etc.

String literals

These types of literals are enclosed within double quotes ("") or using @"". The string constants are similar to character constants and their types. These literals can be represented in multiple lines by enclosing them within double quotes and separated by white spaces. A backslash can be used for representing a continuation of string literals.
"Hey, \
"Hi, Alex"

What Are the Escape Sequences?

Escape sequences are character combination that starts with a backslash trailing with some specific character(s) which in combination form a meaning that is known to the compiler. To represent a new line or printing a single or double quote, or to provide a tab space at the time of program execution, you will use these escape sequences. C# provides these following escape sequences:

  • \' - for giving a single quote.
  • \" - for giving a double quote.
  • \\ - for giving backslash.
  • \0 - for giving Unicode character 0.
  • \a - for giving Alert (character 7).
  • \b - for giving backspace (character 8).
  • \f - for giving form feed (character 12).
  • \n - for giving new-line (character 10).
  • \r - for giving carriage return (character 13).
  • \t - for giving a horizontal tab (character 9).
  • \v - for giving vertical quote (character 11).
  • \uxxxx - for giving Unicode character with hex value xxxx.
  • \xn[n][n][n] - for giving Unicode character with hex value nnnn.
  • \Uxxxxxxxx - for giving Unicode character with hex value xxxxxxxx.

C# Program to Demonstrate All Literals Types


using System;

public class Program {
 public static void Main() {
  int a = 10;
  float pi = 3.14159f;
  char ch = 'g';

  Console.WriteLine("first value is " + a);
  Console.WriteLine("second value is " + pi);
  Console.WriteLine("third value is " + ch);


Hello    C#

first value is 10
second value is 3.14159
third value is g

Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram