C# Constants

Summary: in this tutorial, you will learn about the C# constants and how to use them effectively.

Introduction to the C# constants

In C#, a constant holds a value that is known at compile time and does not change during the execution of the program.

To define a constant, you use the const keyword with the following syntax:

const type ConstantName = value;Code language: C# (cs)

C# only allows you to use built-in types except for the object as constants.

If you define constants inside a method, the constants are only accessible within the method. However, if you define constants in a class, you can use the access modifiers such as public and private to control the accessibility level of the constants.

C# constants examples

Let’s take some examples of using constants.

1) Using C# constants in a method

The following example defines a Converter class with the KgToPound() method that converts weight from kilogram to pound:

// Converter.cs

class Converter
{
    public decimal KgToPound(decimal weight) 
    {
        const decimal factor = 2.205m;

        return weight * factor;

    }
}Code language: C# (cs)

In this KgToPound() method, we define the factor constant with the value 2.205. Because the factor is local to the KgToPound() method, you can only use it inside the method. In other words, you cannot use the factor constant in other methods.

2) Using C# constants in a class

The following example redefines the above Converter class that has the Factor constant defined as a class member:

// Converter.cs

class Converter
{
    public const decimal Factor = 2.205m;

    public decimal KgToPound(decimal weight) 
    {
        return weight * Factor;
    }

    public decimal PoundToKg(decimal weight)
    {
        return weight / Factor;
    }
}Code language: C# (cs)

In this example, since the Factor constant is public, it can be accessed both from methods of the Converter class and from outside of the class.

The following program uses the Converter class to convert the weight from kg to lbs:

// Program.cs

decimal weightInKg, weightInLbs;

var converter = new Converter();

Console.WriteLine($"Convert weight from kg to lbs (factor = {Converter.Factor})");

while(true)
{
    // Prompt to enter a weight in Kg:
    Console.Write("Enter a weight in Kg (0 to exit):");
    weightInKg = Convert.ToDecimal(Console.ReadLine());
    
    if(weightInKg == 0)
    {
        break;
    }
    
    // convert weight from kg to lbs
    weightInLbs = converter.KgToPound(weightInKg);
    Console.WriteLine($"{weightInKg}kg = {weightInLbs}lbs");
}Code language: C# (cs)

Output:

Convert weight from kg to lbs (factor = 2.205)
Enter a weight in Kg (0 to exit):1
1kg = 2.205lbs
Enter a weight in Kg (0 to exit):2
2kg = 4.410lbs
Enter a weight in Kg (0 to exit):2.5
2.5kg = 5.5125lbs
Enter a weight in Kg (0 to exit):0Code language: plaintext (plaintext)

Summary

  • C# constants are identifiers that hold values that are known at compile-time and do not change during the execution of the program.
Was this tutorial helpful ?