Const vs Readonly in C#



This content originally appeared on DEV Community and was authored by Sharad Aade

const vs readonly

Const

  • ** Const is compiled time constant**
  • Declaration and initialization at the same time
  • Can not be changed public const int cmToMeter = 100;

Readonnly

  • Readonly is runtime constant
  • Decalaration and initialization not mandatory in same time we can intialise later on
  • We can change the value

public static readonly double PI = 3.1;
public static readonly double PI;
PI = 3.14


This content originally appeared on DEV Community and was authored by Sharad Aade