A Comprehensive Resource for Microsoft Technologies

Welcome, your go-to destination for everything related to .NET and Microsoft technologies. With over 10 years of experience in the IT industry, I am excited to share my knowledge and insights on this platform. This blog is dedicated to providing valuable information, tips, and tutorials that are not only relevant to the rapidly evolving IT industry but also essential for individuals working on real-time projects and preparing for interviews

C# Tutorial 3.3 : Constants

 In C#, constants are used to represent values that do not change during the execution of a program. They provide a way to define values that remain constant throughout the program's lifecycle. Let's dive into how to declare and use constants in C#.

1. Declaring Constants:

In C#, you declare a constant using the const keyword followed by the data type, the constant name, and the value.

using System;

class Program
{
    // Declaring integer constant
    const int MaxAttempts = 3;

    static void Main()
    {
        // Attempt to change the value of a constant (will result in a compilation error)
        // MaxAttempts = 5;

        // Displaying the value of the constant
        Console.WriteLine($"Maximum Attempts Allowed: {MaxAttempts}");

        // Other code...
    }
}

In this example, MaxAttempts is a constant of type int with a value of 3. Note that attempting to change the value of a constant after it has been assigned will result in a compilation error.

2. Benefits of Constants:

  • Readability: Constants improve code readability by providing meaningful names for values used in your program.

  • Maintenance: If a value needs to be changed, you only need to update it at the constant declaration, and the change will be reflected throughout the code.

3. Constants in Different Scopes:

Constants can be declared at various levels, including within a method, inside a class, or even at the namespace level. The scope of a constant determines where it can be accessed.

using System;

class Program
{
    // Class-level constant
    const double Pi = 3.14159265359;

    static void Main()
    {
        // Method-level constant
        const int MaxValue = 100;

        // Displaying the values
        Console.WriteLine($"Pi: {Pi}");
        Console.WriteLine($"Max Value: {MaxValue}");

        // Other code...
    }
}


In this example, Pi is a class-level constant, and MaxValue is a method-level constant.

4. Naming Conventions for Constants:

By convention, constant names are written in uppercase letters with underscores separating words.

const int MAX_VALUE = 100;

5. Run the Program:

Press F5 or click the "Start Debugging" button to build and run your program. The output should display the values of the constants.

6. Experiment:

  • Declare constants of different data types (int, double, string, etc.).
  • Try using constants in different scopes to understand their accessibility.

Congratulations! You've learned how to declare and use constants in C#. In the next tutorial, we'll explore control flow statements, starting with if statements.