Skip to content

Const Fields

Mario Gutierrez edited this page Jan 7, 2017 · 1 revision
  • A const field must be assigned at the time of the declaration.
  • A readonly field can be assigned at declaration or in the constructor.

const is implicitly static.

public const int PI = 3.1415f;

readonly is an instance variable.

public readonly DateTime INITIALIZED;

public MyBrand()
{
  INITIALIZED = DateTime.Now;
}

static can be used with readonly, which is almost like const

public static readonly DateTime FIRST_INITIALIZATION;

static MyBrand()
{ // The difference is you can use a static constructor to initialize it.
  FIRST_INITIALIZATION = DateTime.Now;
}
Clone this wiki locally