-
C# Constants
-
In C#, a constant is a variable whose value cannot be changed after it is initialized. Constants are defined using the
const
keyword and are typically used for values that are known at compile time and won't change during the execution of the program.Meaning of Constants:
- A constant is a
read-only
variable, but the key difference is that its value is set at compile time, meaning the compiler will substitute the constant with its value wherever it is used. - The value assigned to a constant is determined at the compile time and cannot be modified at runtime.
Key Points about Constants:
- Type Specification: Constants must have a value assigned at the time of declaration and cannot be left uninitialized.
- Compile-Time Evaluation: The value of a constant is evaluated at compile time, making it more efficient for values that don't change.
- Scope: Constants can be defined at the class or method level and can have different access modifiers (e.g., public, private) .
- Naming Convention: By convention, constant names are written in
uppercase
letters, using underscores to separate words (e.g., PI, MAX_LENGTH).
Syntax for defining a constant is:
const data_type constant_name = value;
The following program demonstrates defining and using a constant in your program −
Example 1: Basic Constant
using System; class Program { // Declaring a constant const double PI = 3.14159; static void Main() { Console.WriteLine("The value of PI is: " + PI); } }
When the above code is compiled and executed, it produces the following result −
Output:
The value of PI is: 3.14159
In this example,
PI
is a constant with the value3.14159
. Its value cannot be changed during the program execution.Example 2: Constants with Different Data Types
using System; class Program { // Declaring constants of different types const int MaxItems = 10000; const string GreetingMessage = "Hello, World!"; static void Main() { Console.WriteLine("Max Items: " + MaxItems); Console.WriteLine(GreetingMessage); } }
Output:
Max Items: 10000 Hello, World!
Here,
MaxItems
is an integer constant with a value of10000
, andGreetingMessage
is a string constant with the value"Hello, World!"
.Example 3: Constants in a Class
using System; class Calculator { // Constant for tax rate public const double TAX_RATE = 0.07; public static double CalculateTax(double amount) { return amount * TAX_RATE; } } class Program { static void Main() { double price = 20000.0; double tax = Calculator.CalculateTax(price); Console.WriteLine("The tax on $" + price + " is: $" + tax); } }
Output
The tax on $20000 is: $1400
In the above example, the constant
TAX_RATE
is used in theCalculator
class to calculate the tax based on the price.Conclusion:
- Constants are useful for values that should remain unchanged throughout the program's execution.
- They offer better performance and code readability.
- Constants must be initialized at the time of declaration, and their values cannot be modified after that.
- A constant is a