Search This Blog

Sunday, June 15, 2014

Difference between Constant and ReadOnly in C#

Constant:
--------

  1. Constant value is declared using "const" keyword.
  2. A const needs to be declared and initialized at declaration only.
  3. A const’s value is evaluated at design time.
  4. A const can not be static.
const string name = "Jeetendra";

ReadOnly:
--------

  1. ReadOnly value is declared using "readonly" keyword.
  2. A readonly can be initialized at declaration or by the code in the constructor.
  3. A readonly’s value is evaluated at runtime.
  4. A readonly can be static.
readonly string name = "Jaiswal";

No comments:

Post a Comment