What's difference between constants and static readonly?
By in ASP.NET on Jun 02 2007
  • Pramod Verma
    Jan, 2015 24

    Constants : Constant value is initialized at compile time. Const int a=10;static Readonly : Static Readonly Initialize in static constructor only.

    • 0
  • sudhakar sakthivel
    Sep, 2009 1

    readonly: This is the Runtime constant.  We can initialize the values once through constructor(while running).

    constant: Constant value is initialized at compile time.  We can not initialize the constant values at compile time.

    • 0
  • prashanth
    Jun, 2007 5

    A const must be initialized at the time of its creation. A readonly field can be assigned to once in the class constructor allowing you to pass in the value at run-time. Declaring fields as const protects both you and other programmers from accidentally changing the value of the field. Also note that with const fields, the compiler performs some optimization by not declaring any stack space for the field. The readonly keyword is similar to const, with two exceptions. First, the storage of a readonly field is the same as a regular read-write field, and thus there is no performance benefit. Secondly, readonly fields can be initialized in the constructor of the containing class.

    • 0
  • Jun, 2007 2

    Constants :- it's value set at compile time and support value types Static readonly:it's value set at run time and are able to hold refrence types

    • 0


Most Popular Job Functions

MOST LIKED QUESTIONS

var $j = jQuery.noConflict();