1.

Readonly Vs. Const?

Answer»

A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a CONSTRUCTOR.Therefore,readonly FIELDS can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime CONSTANTS, as in the following example:

PUBLIC static readonly UINT l1 = (uint) DateTime.Now.Ticks;

A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor.Therefore,readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants, as in the following example:

public static readonly uint l1 = (uint) DateTime.Now.Ticks;



Discussion

No Comment Found