Constants and Literals
Vaibhav • September 11, 2025
In every program, some values never change. These are the fixed facts your code relies on - like the number of days in a week, the name of your application, or a welcome message. In C#, such values are called constants. Alongside constants, we also use literals - the raw values we write directly into our code. This article introduces both concepts, explains how to use them, and shows why they matter for clarity, safety, and maintainability.
What is a constant?
A constant is a variable whose value cannot change once it’s set. In C#, you declare a constant using the const
keyword. This tells the compiler: “This value is fixed. Don’t let anyone
modify it.”
const int DaysInWeek = 7;
const string AppName = "MyApp";
const bool IsProduction = false;
In this example:
DaysInWeek
is a numeric constant.AppName
is a string constant.IsProduction
is a Boolean constant.
These values are baked into the program at compile time. You cannot change them later - not even by accident. If you try, the compiler will stop you.
Constants must be initialized when declared. You cannot declare a constant and assign its value later. This ensures the value is always known and fixed.
Why use constants?
Constants make your code safer and easier to understand. Instead of repeating the same value in multiple places, you define it once and give it a meaningful name. This reduces errors and makes updates easier.
// Without constant
Console.WriteLine("Welcome to MyApp");
Console.WriteLine("MyApp is starting...");
// With constant
const string AppName = "MyApp";
Console.WriteLine($"Welcome to {AppName}");
Console.WriteLine($"{AppName} is starting...");
In the second version, we use AppName
instead of repeating the string "MyApp"
. If the name ever changes, we only need to update it in one place.
Use constants for values that are fixed by definition - like limits, labels, or configuration flags. This improves readability and prevents accidental changes.
What is a literal?
A literal is a raw value written directly into your code. You’ve already seen many literals in previous articles - numbers, strings, and Boolean values. Here are some examples:
int age = 25; // 25 is a numeric literal
string name = "Alice"; // "Alice" is a string literal
bool isReady = true; // true is a Boolean literal
Literals are the building blocks of values. They appear in assignments, messages, and calculations. While they’re useful, overusing them can make code harder to maintain - especially if the same literal appears in many places.
Types of literals
C# supports several kinds of literals. Let’s look at the ones we’ve already used:
Numeric literals
These represent numbers. You can write them as whole numbers (int
) or with
decimals (float
, double
, decimal
- covered later).
int score = 100;
long population = 7800000000;
byte level = 5;
Each literal matches the type of the variable. If the value is too large or too small, the compiler will show an error.
String literals
These are sequences of characters enclosed in double quotes. You’ve used them in greetings, labels, and messages.
string greeting = "Hello, world!";
string empty = "";
You can include special characters using escape sequences - like \n
for newline
or \t
for tab. These were covered in the previous article on strings.
Boolean literals
These represent truth values: true
and false
.
They’re used to track status, toggle features, or guide decisions.
bool isLoggedIn = false;
bool hasPermission = true;
Boolean literals are not strings or numbers. They are distinct keywords with special meaning in logic and conditions.
You cannot assign a string like "true"
to a Boolean
variable. The value must be the keyword true
or false
.
Constants vs literals
Literals are used once. Constants are used many times. If a value appears only once and is unlikely to change, a literal is fine. But if the same value appears in multiple places - or might change later - use a constant.
// Using literal
Console.WriteLine("Welcome to MyApp");
Console.WriteLine("MyApp is starting...");
// Using constant
const string AppName = "MyApp";
Console.WriteLine($"Welcome to {AppName}");
Console.WriteLine($"{AppName} is starting...");
The second version is easier to maintain. If the app name changes, you only update AppName
. The rest of the code stays the same.
Where to place constants
For now, you can place constants at the top of your program - before the Main
method. Later, when we introduce classes, you’ll learn how to organize constants more formally.
const string AppName = "MyApp";
static void Main()
{
Console.WriteLine($"Welcome to {AppName}");
}
This structure keeps your constants visible and easy to find. It also separates configuration from logic - a good habit for clean code.
Common mistakes and how to avoid them
One common mistake is trying to change a constant after it’s declared. For example:
const int MaxUsers = 100;
MaxUsers = 200; // ❌ Error: Cannot assign to a constant
The compiler will reject this. Constants are fixed - that’s the point.
Another mistake is using literals repeatedly instead of defining a constant. This makes your code harder to update and understand.
// Repeated literal
Console.WriteLine("Welcome to MyApp");
Console.WriteLine("MyApp is starting...");
Console.WriteLine("MyApp is shutting down...");
// Better with constant
const string AppName = "MyApp";
Console.WriteLine($"Welcome to {AppName}");
Console.WriteLine($"{AppName} is starting...");
Console.WriteLine($"{AppName} is shutting down...");
The second version is cleaner, safer, and easier to maintain.
Constants are replaced by their values during compilation. This means they don’t exist as variables at runtime - they’re just fixed values in the final executable.
Constants in algorithms
In Chapter 1, we introduced algorithms as step-by-step instructions. Constants often guide these steps. For example, if you’re building a program that calculates age in days, you might define a constant for the number of days in a year:
const int DaysInYear = 365;
int age = 25;
int ageInDays = age * DaysInYear;
Console.WriteLine($"You are approximately {ageInDays} days old.");
This use of a constant makes the algorithm clearer and more accurate. If you ever switch to leap years or use a different calendar, you can update the constant without changing the logic.
Summary
Constants and literals are essential tools for expressing values in C#. A literal is a raw
value written directly into your code - like 25
, "Hello"
, or true
. A constant is
a named value that never changes, declared using the const
keyword. Constants
improve readability, prevent errors, and make your code easier to maintain.
In this article, you learned how to declare constants, use literals, and decide when each is appropriate. You saw how constants help organize your code, reduce repetition, and clarify intent. As your programs grow, these small habits will make a big difference in quality and clarity.