Difference between a Const and Readonly

Posted by Techie Cocktail | 12:28 AM | | 1 comments »

Below are some differences:

Const
a. const member field must be initialized at the time of declaration.
b. const field can have only one value which is defined at the time of declaration.
c. const can't be static.

Readonly
a. readonly field can be initialized at the time of declaration or in the class's consructor.
b. The value of the readonly field once set at the time of declaration, can be updated at runtime.
c. readonly can be either instance-level or static.

Look at a const field to be a compile-time constant and readonly field as a runtime constant.

1 comments

  1. Anonymous // September 3, 2009 at 8:56 AM  

    short and to the point. i like it.