VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/c-sharp-literals/

⇱ C# Constants & Literals - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C# Constants & Literals

Last Updated : 20 Apr, 2026

A constant is a variable whose value cannot be changed once assigned. It is defined using the "const" keyword and is used to store fixed values in a program these values are called literals. These values are assigned to variables and remain unchanged during program execution.


Output
24
100

In the above code, the variable 'c' is a constant whose value cannot be changed. The value 100 assigned to variable b is a literal, which is directly written in the program.

Constants in C#

A constant is a named value that remains unchanged throughout the program. It must be initialized at the time of declaration using the const keyword. Constants are useful when a value should not be modified during execution. Let's take a look at an example:


Output
24

In the above code, the variable c is a constant whose value cannot be changed. Its value is determined at compile time and embedded directly into the program.

Types of Constants in C#

  1. Integer Constant: Used to store fixed whole number values. It is declared using const with integer type. The value cannot be changed after initialization.
  2. Floating Constant: Used to store fixed decimal values. It can be of type float or double. Float constants require a suffix 'f'.
  3. Character Constant: Used to store a single fixed character. It is enclosed in single quotes (' '). The value remains unchanged during execution.
  4. String Constant: Used to store fixed text values. It is enclosed in double quotes (" "). The value cannot be modified after assignment.
  5. Boolean Constant: Used to store logical values such as true or false. It is mainly used in conditional statements. The value remains constant.

Output
10
3.14
A
Hello
True

Literals in C#

A literal represents a fixed value written directly in the program. It does not have a name and is used to assign values to variables during initialization. Literals remain unchanged during execution. Let's take a look at an example:


Output
100

In the above code, the value 100 assigned to the variable x is a literal. It is a fixed value directly written in the program and used to initialize the variable. This value remains constant during program execution.

Types of Literals in C#

  1. Integer Literals: Represents whole numbers directly written in the code. It can be decimal, hexadecimal, or binary. By default, it is of type int.
  2. Floating-point Literals: Represents decimal values written in the code. By default, it is of type double. It can be defined as float using suffix f.
  3. Character Literals: Represents a single character enclosed in single quotes. It can also be defined using Unicode or escape sequences.
  4. String Literals: Represents a sequence of characters enclosed in double quotes. It is used to store text directly in the code.
  5. Boolean Literals: Represents logical values directly in the code. Only two values are allowed: true and false.
  6. Null Literals: Represents the absence of any value. It is mainly used with reference types.

Output
100
3.14
A
Hello
True

Difference Between Constants and Literals

Constants and literals are often confused as the same, but in C#, they are different entities with distinct meanings.

Features

Constants

Literals

Definition

A constant is a named variable whose value cannot change.

A literal is a fixed value directly written in the code.

Declaration

Declared using the const keyword.

No declaration required.

Storage

Stored in memory with a name (identifier).

Used directly without any name.

Example

const int x = 10;

10, "Hello", true

Comment
Article Tags:
Article Tags:

Explore