C Sharp Constants

C# Constants – Complete Beginner Guide
Understanding C# constants is essential for writing safe, reliable, and maintainable programs.
Constants allow you to store values that cannot change during program execution. They improve code clarity, prevent accidental modifications, and make your applications more predictable.
In this complete beginner guide, you’ll learn:
What constants are in C#
Why constants are important
How to declare constants
Rules for using const
Difference between const and readonly
Naming conventions
Practical examples
Common beginner mistakes
Best practices
Let’s get started
What Is a Constant in C#?
A constant is a variable whose value cannot change after it is assigned.
In C#, constants are declared using the const keyword.
Once defined, their value remains fixed throughout the program.
Basic Example
1 | const double PI = 3.14159; |
Here:
const→ keyworddouble→ data typePI→ constant name3.14159→ fixed value
If you try to change PI later, the compiler will generate an error.
Why Are Constants Important?
Constants are important because they:
Prevent accidental value changes
Improve code readability
Make programs safer
Represent fixed values like tax rates or mathematical constants
Make maintenance easier
For example, instead of writing:
1 | double area = 3.14159 * radius * radius; |
You should write:
1 2 | const double PI = 3.14159; double area = PI * radius * radius; |
This makes the code clearer and easier to maintain.
Syntax of C# Constants
General Syntax:
1 | const dataType CONSTANT_NAME = value; |
Example:
1 2 | const int MaxStudents = 50; const string CompanyName = "Vipul HVAC"; |
Important:
A constant must be initialized when declared.
You cannot declare a constant without assigning a value.
Wrong:
1 | const int number; |
Correct:
1 | const int number = 10; |
Rules for Declaring Constants in C#
Here are important rules:
- Must use the
constkeyword - Must assign value at declaration
- Value cannot change later
- Must be compile-time constant
- Cannot use dynamic values
Constants Must Be Compile-Time Values
Constants must be known at compile time.
Valid:
1 2 | const int number = 100; const double TaxRate = 0.18; |
Invalid:
1 2 | int age = 20; const int myAge = age; // Error |
Because age is not a compile-time constant.
Naming Conventions for Constants
C# commonly uses PascalCase for constants.
Example:
1 2 | const double InterestRate = 0.05; const int MaxUsers = 100; |
Some developers use ALL CAPS:
1 | const double INTEREST_RATE = 0.05; |
However, PascalCase is more common in modern C# coding standards.
Real Beginner Example Using Constants
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | using System; class Program { static void Main() { const double PI = 3.14159; double radius = 5; double area = PI * radius * radius; Console.WriteLine("Area of Circle: " + area); } } |
This example shows:
Proper constant declaration
Usage in calculation
Improved readability
Difference Between const and readonly in C#
Beginners often confuse these two.
const
Value fixed at compile time
Must be assigned immediately
Cannot change anywhere
readonly
Value assigned at runtime
Can be assigned in constructor
Cannot change after initialization
Example of readonly:
1 2 3 4 5 6 7 8 9 | class Program { readonly int number; public Program(int num) { number = num; } } |
Key Difference:
const→ compile-time constantreadonly→ runtime constant
For beginners, start with const.
Constants vs Variables
| Feature | Variable | Constant |
|---|---|---|
| Value change allowed | Yes | No |
| Keyword | data type only | const |
| Must initialize immediately | No | Yes |
| Compile-time value | Not required | Required |
Example:
1 2 | int age = 20; // variable const int year = 2026; // constant |
When Should You Use Constants?
You should use constants for:
Mathematical values (PI)
Fixed configuration values
Tax rates
Maximum limits
Application settings
Fixed strings
Avoid using constants for values that may change in future.
Common Beginner Mistakes with Constants
- Trying to change constant value
- Not assigning value during declaration
- Using non-constant expression
- Confusing const with readonly
- Poor naming conventions
Example mistake:
1 2 | const int age = 20; age = 25; // Error |
Constants cannot be modified.
Constants with Different Data Types
You can create constants for many data types.
1 2 3 4 5 | const int MaxScore = 100; const double Gravity = 9.8; const char Grade = 'A'; const string Country = "India"; const bool IsActive = true; |
Note:
String constants must be known at compile time.
Using Constants in Calculations
Constants work like normal variables in calculations.
Example:
1 2 3 4 5 | const double DiscountRate = 0.10; double price = 1000; double finalPrice = price - (price * DiscountRate); Console.WriteLine("Final Price: " + finalPrice); |
This improves clarity compared to using raw numbers.
Best Practices for Using Constants
- Use constants for fixed values
- Give meaningful names
- Follow PascalCase
- Avoid magic numbers
- Group related constants
- Document important constants
What Are Magic Numbers?
Magic numbers are hard-coded values without explanation.
Bad example:
1 | double result = value * 9.8; |
Better:
1 2 | const double Gravity = 9.8; double result = value * Gravity; |
Constants eliminate magic numbers.
Benefits of Using Constants
Using constants:
Makes code readable
Reduces bugs
Improves maintainability
Enhances clarity
Makes debugging easier
Professional developers always prefer constants over repeated hard-coded values.
Frequently Asked Questions (FAQs)
1. What is a constant in C#?
A constant is a value declared with the const keyword that cannot be changed during program execution.
2. Can I change a constant after declaring it?
No. Constants are fixed and cannot be modified.
3. What is the difference between const and readonly?
const is fixed at compile time, while readonly is assigned at runtime and cannot change afterward.
4. Must constants be initialized?
Yes. Constants must be assigned a value when declared.
5. Should I use constants instead of variables?
Use constants when the value should never change.
Final Thoughts
C# constants are:
Safe
Reliable
Clear
Professional
Mastering constants helps you:
Avoid magic numbers
Write clean code
Prevent accidental errors
Improve program structure
Constants are small but powerful tools in C# programming.
