Wednesday, March 24, 2010

Static Readonly vs Constant

Static ReadOnly >> Static readonly is a class level variable that can't be accessed using the class object. It's value can bedefined at the runtime using any expression or any run time variable only in the static constructor of the class.

Constant >> Constants are also class level variables as they are implicitly static but they can be set only once (at the time of declaration only). You can't set a constant at the runtime.

If you want to define your field at design time then you must use constant but if the value of the field is decided at the runtime then you have the only option to use Static readonly.

No comments:

Post a Comment