Difference between static, readonly, and constant in C#

0

 

Difference between static, readonly, and constant in C#

In C#, there are three types of variables that are used to store values that cannot be changed during program execution: static, readonly, and constant. Although these three types of variables have some similarities, they also have some key differences that developers need to understand.

In this blog post, we will discuss the difference between static, readonly, and constant in C#.

Static Variables

Static variables are those variables that belong to the class, rather than to an instance of the class. This means that there is only one copy of the variable shared by all instances of the class. Static variables are initialized only once, when the class is first loaded into memory.

One common use of static variables is to keep track of how many instances of a class have been created. For example, consider the following code:


public class MyClass
{
    private static int count = 0;

    public MyClass()
    {
        count++;
    }

    public static int Count
    {
        get { return count; }
    }
}

In this example, the static variable count is used to keep track of how many instances of MyClass have been created. Each time a new instance of MyClass is created, the constructor increments the value of count.

Readonly Variables

Readonly variables are those variables that can only be assigned a value once. Once a value has been assigned, it cannot be changed. Readonly variables can be declared at the class level or at the instance level.

One common use of readonly variables is to store a value that is calculated at runtime but never changes. For example, consider the following code:


public class Circle
{
    private readonly double _radius;

    public Circle(double radius)
    {
        _radius = radius;
    }

    public double Area
    {
        get { return Math.PI * _radius * _radius; }
    }
}

In this example, the readonly variable _radius is assigned a value in the constructor and then used to calculate the area of the circle. Because the value of _radius never changes, it is marked as readonly.

Constant Variables

Constant variables are those variables that cannot be changed at all. Unlike static and readonly variables, constant variables are always implicitly static, which means they belong to the class and not to an instance of the class.

One common use of constant variables is to define a value that is used throughout the application and is never meant to change. For example, consider the following code:


public class MathConstants
{
    public const double Pi = 3.14159265358979323846;
}


In this example, the constant variable Pi is defined with the value of pi. This value is used throughout the application and is never meant to change.

Key Differences between Static, Readonly, and Constant

The main differences between static, readonly, and constant variables are:

  • Static variables are shared by all instances of a class, while readonly and constant variables are specific to each instance.
  • Readonly variables can only be assigned a value once, while constant variables cannot be assigned a value at all.
  • Readonly variables can be assigned a value at runtime, while constant variables must be assigned a value at compile time.
  • Constant variables are always implicitly static, while static and readonly variables can be either static or instance-level.


In summary, static, readonly, and constant variables are all used to store values that cannot be changed during program execution. While they share some similarities, each type of variable has its own specific use case and should be used appropriately. Understanding the differences between these types of variables is key to writing clean and efficient C# code.


Tags

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !