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.
// constant
const int c = 24;
int b = 100; // literal
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:
using System;
class Program
{
static void Main()
{
// Declaring a constant
const int c = 24;
Console.WriteLine(c);
}
}
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#
- Integer Constant: Used to store fixed whole number values. It is declared using
constwith integer type. The value cannot be changed after initialization. - Floating Constant: Used to store fixed decimal values. It can be of type
floatordouble. Float constants require a suffix 'f'. - Character Constant: Used to store a single fixed character. It is enclosed in single quotes (
' '). The value remains unchanged during execution. - String Constant: Used to store fixed text values. It is enclosed in double quotes (
" "). The value cannot be modified after assignment. - Boolean Constant: Used to store logical values such as true or false. It is mainly used in conditional statements. The value remains constant.
using System;
class Program
{
static void Main()
{
const int a = 10;
// Integer Constant
const double b = 3.14;
// Floating Constant
const char c = 'A';
// Character Constant
const string s = "Hello";
// String Constant
const bool flag = true;
// Boolean Constant
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine(s);
Console.WriteLine(flag);
}
}
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:
using System;
class Program{
static void Main(){
int x = 100;
Console.WriteLine(x);
}
}
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#
- Integer Literals: Represents whole numbers directly written in the code. It can be decimal, hexadecimal, or binary. By default, it is of type int.
- 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.
- Character Literals: Represents a single character enclosed in single quotes. It can also be defined using Unicode or escape sequences.
- String Literals: Represents a sequence of characters enclosed in double quotes. It is used to store text directly in the code.
- Boolean Literals: Represents logical values directly in the code. Only two values are allowed: true and false.
- Null Literals: Represents the absence of any value. It is mainly used with reference types.
using System;
class Program
{
static void Main()
{
int x = 100;
// Integer Literal
double d = 3.14;
// Floating-point Literal
char c = 'A';
// Character Literal
string s = "Hello";
// String Literal
bool b = true;
// Boolean Literal
string a = null;
// Null Literal
Console.WriteLine(x);
Console.WriteLine(d);
Console.WriteLine(c);
Console.WriteLine(s);
Console.WriteLine(b);
Console.WriteLine(a);
}
}
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 |