Skip to main content

What is the difference between const and readonly?



Const
Readonly
A constant value will be embedded in the executable file at compile time, 
A read only variable is read by the program at run time. 
Initiailized at declaration only.
Can be initialized in declaration or by code in the constructor.
Can't be static.
Can be either instance-level or static.
Value is evaluated at compile time.
Value is evaluated at run time.

Readonly Example

public class ClassA
{
    public readonly double PI;

    public ClassA()
    {
        PI = 3.14159;
    }
}


Const Example

public class ClassA
{
    public const double PI = 3.14159;
}

Comments