Section 2. Fundamentals. 2.3 C# Constants and Literals

The constants refer to unchangeable and read-only (fixed) values that the program may not alter during its execution. They are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are also enumeration constants as well.

The constants are treated just like regular variables except that their values cannot be modified after their definition.

You cannot declare a constant variable without assigning the value. If you do, an error will occur: A const field requires a value to be provided.

Integer Literals

An integer literal can be a decimal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, and there is no prefix id for decimal.

An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.

Following are other examples of various types of Integer literals −

  • 45 /* decimal /
  • 0x2a / hexadecimal /
  • 20 / int /
  • 10u / unsigned int /
  • 10l / long /
  • 10ul / unsigned long */

Floating-point Literals

A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form.

While representing in decimal form, you must include the decimal point, the exponent, or both; and while representing using exponential form you must include the integer part, the fractional part, or both. The signed exponent is introduced by e or E.

The examples of floating-point literals:

  • 3.14159
  • 314159E-5F

Character Constants

Character literals are enclosed in single quotes. For example, ‘x’ and can be stored in a simple variable of char type. A character literal can be a plain character (such as ‘x’), an escape sequence (such as ‘\t’), or a universal character (such as ‘\u02C0’).

There are certain characters in C# when they are preceded by a backslash. They have special meaning and they are used to represent like newline (\n) or tab (\t).

Escape sequenceMeaning
\\\ character
\’‘ character
\”” character
\?? character
\aAlert or bell
\fForm feed
\rCarriage return
\tHorizontal tab
\vVertical tab
\xhh . . .Hexadecimal number of one or more digits

String Literals

String literals or constants are enclosed in double quotes “” or with @””. A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.

You can break a long line into multiple lines using string literals and separating the parts using whitespaces.

Here are some examples of string literals. All the three forms are identical strings.

"hello, world"
"hello, \
"hello, " "w" "orld"

Defining Constants

Constants are defined using the const keyword.

Syntax for defining a constant is:

const <data_type> <constant_name> = value;

Wrapping out

We have learned about C# constants and literals.

Thank you for reading.

Check for more tutorials at

Check for Arduino and Raspberry Pi projects on our website

Section 1. Introduction. 1.1 C# programming language

Section 1. Introduction. 1.2 Introduction to .NET Framework

Section 1. Introduction. 1.3 C# versions history

Section 1. Introduction. 1.4 C# vs Java

Section 1. Introduction. 1.5 C# get started

Section 1. Introduction. 1.6 Your first program – Hello world

Section 1. Introduction. 1.7 C# identifiers and keywords

Section 2. Fundamentals. 2.1 C# Comments

Section 2. Fundamentals. 2.2 C# Data types

Section 2. Fundamentals. 2.3 C# Constants and Literals