Skip to main content
clarifying declaration of const as static is not allowed, cleanup of markup
Source Link

A constantconstant member is defined at compile time and cannot be changed at runtime. ConstantsConstants are declared as a field, using the constconst keyword and must be initialized as they are declared.

public class MyClass
{
    public const double PI1 = 3.14159;
}

A readonly member is like a constantconstant in that it represents an unchanging value. The difference is that a readonly member can be initialized at runtime, in a constructorconstructor, as well being able to be initialized as they are declared.

public class MyClass1
{
     public readonly double PI2 = 3.14159;

     //or

     public readonly double PI3;

     public MyClass2()
     {
         PI3 = 3.14159;
     }
}

const Vs readonly,

const,

  • They Cancan not be declared as static (they are implicitly static)
  • The value of constant is evaluated at compile time
  • constants are initiailizedinitialized at declaration only

readonly,

  • They Cancan be either instance-level or static
  • The value is evaluated at run time
  • readonly Cancan be initialized in declaration or by code in the constructor

A constant member is defined at compile time and cannot be changed at runtime. Constants are declared as a field, using the const keyword and must be initialized as they are declared.

public class MyClass
{
    public const double PI1 = 3.14159;
}

A readonly member is like a constant in that it represents an unchanging value. The difference is that a readonly member can be initialized at runtime, in a constructor as well being able to be initialized as they are declared.

public class MyClass1
{
     public readonly double PI2 = 3.14159;

     //or

     public readonly double PI3;

     public MyClass2()
     {
         PI3 = 3.14159;
     }
}

const Vs readonly,

const,

  • They Can not be static
  • The value of constant is evaluated at compile time
  • constants are initiailized at declaration only

readonly,

  • They Can be either instance-level or static
  • The value is evaluated at run time
  • readonly Can be initialized in declaration or by code in the constructor

A constant member is defined at compile time and cannot be changed at runtime. Constants are declared as a field, using the const keyword and must be initialized as they are declared.

public class MyClass
{
    public const double PI1 = 3.14159;
}

A readonly member is like a constant in that it represents an unchanging value. The difference is that a readonly member can be initialized at runtime, in a constructor, as well being able to be initialized as they are declared.

public class MyClass1
{
     public readonly double PI2 = 3.14159;

     //or

     public readonly double PI3;

     public MyClass2()
     {
         PI3 = 3.14159;
     }
}

const

  • They can not be declared as static (they are implicitly static)
  • The value of constant is evaluated at compile time
  • constants are initialized at declaration only

readonly

  • They can be either instance-level or static
  • The value is evaluated at run time
  • readonly can be initialized in declaration or by code in the constructor
Source Link
Sujit
  • 3.7k
  • 9
  • 43
  • 50

A constant member is defined at compile time and cannot be changed at runtime. Constants are declared as a field, using the const keyword and must be initialized as they are declared.

public class MyClass
{
    public const double PI1 = 3.14159;
}

A readonly member is like a constant in that it represents an unchanging value. The difference is that a readonly member can be initialized at runtime, in a constructor as well being able to be initialized as they are declared.

public class MyClass1
{
     public readonly double PI2 = 3.14159;

     //or

     public readonly double PI3;

     public MyClass2()
     {
         PI3 = 3.14159;
     }
}

const Vs readonly,

const,

  • They Can not be static
  • The value of constant is evaluated at compile time
  • constants are initiailized at declaration only

readonly,

  • They Can be either instance-level or static
  • The value is evaluated at run time
  • readonly Can be initialized in declaration or by code in the constructor