Understanding the Differences between Static, Readonly, and Constant in C#

Category > CSHARP || Published on : Friday, March 10, 2023 || Views: 230 || C# programming static readonly constant


In C#, there are three keywords that are used to define values that cannot be changed during program execution: static, readonly, and constant. Each of these keywords has its unique properties and use cases. This article will explain the differences between these keywords and how to use them in C# programming.

C# is a popular object-oriented programming language that is widely used in developing various software applications. In C#, there are three keywords that are used to define values that cannot be changed during program execution: static, readonly, and constant. In this article, we will explore the differences between these keywords and how they can be used in C# programming.

Static Keyword

The static keyword is used to define a member that belongs to a type, rather than an instance of that type. This means that the value of a static member is shared by all instances of the type, and it can be accessed without creating an instance of the type.

Static members can be fields, properties, methods, and nested types. They are initialized only once, when the type is first used, and they retain their values throughout the life of the application. This makes them useful for storing values that are common to all instances of a type, such as a count of how many instances have been created.

Here is an example of a static field:

class Example
{
    public static int Count = 0;
}

// ...

Example.Count++;

In this example, we define a static field called Count in the Example class. We can access this field without creating an instance of the Example class, and we can use it to keep track of how many instances of the class have been created.

Readonly Keyword

The readonly keyword is used to define a value that cannot be changed after it is initialized. This means that the value of a readonly field can be set only in the constructor of the class or in a field initializer.

Readonly fields are useful for storing values that are constant throughout the life of an object, such as a connection string to a database. They are different from const fields in that they can be initialized at runtime, whereas const fields must be initialized at compile time.

Here is an example of a readonly field:

class Example
{
    public readonly int Value;

    public Example(int value)
    {
        Value = value;
    }
}

// ...

Example example = new Example(42);

In this example, we define a readonly field called Value in the Example class. The value of Value can be set only in the constructor of the class, and once it is set, it cannot be changed. We create a new instance of the Example class and set the value of Value to 42.

Constant Keyword

The const keyword is used to define a value that cannot be changed during program execution. This means that the value of a const field is set at compile time and cannot be changed at runtime.

Const fields are useful for storing values that are known at compile time and are not likely to change, such as the value of pi.

Here is an example of a const field:

class Example
{
    public const double Pi = 3.14159;
}

// ...

double radius = 10;
double area = Example.Pi * radius * radius;

In this example, we define a const field called Pi in the Example class. The value of Pi is set at compile time and cannot be changed at runtime. We use the value of Pi to calculate the area of a circle with a radius of 10.

Conclusion

In summary, the static, readonly, and const keywords are used to define values that cannot be changed during program execution. Static members are shared by all instances of a type, readonly fields can be set only in the constructor or in a field initializer, and const fields must be initialized at compile time