Sunday, 15 February 2015

c# - Static readonly variables initialization -



c# - Static readonly variables initialization -

i have dictionary objects doesn't alter during application life time. planning utilize static readonly variables, provide inputs on below points.

difference between initializing straight static readonly properties , static properties operator backed private readonly static variables.

is there risk using them have read online not utilize public static variables. apply situation.

i have dictionary objects doesn't alter during application life time.

when mark variable of dictionary type readonly, prevent replacement of dictionary assign dictionary. not create dictionary read-only, in sense caller gets hands on dictionary, free alter in way wants, wiping clean, or setting wrong values (by mistake, no doubt). if need create dictionary read-only, consider borrowing implementation of read-only wrapper from answer.

in general, advantage of adding property on top of variable, or having {get;private set;} automatic property, on readonly static ability perform additional checks in setter, or adding code in getter (say, collect access statistics or logging). there implications accessing field through reflection. not doing of that, exposing readonly variable sounds appropriate , not nowadays additional risks.

edit: (on using reflection) when access object info through reflection, must specify if accessing property xyz or field xyz. in contrast, when write c# program, write someclass.xyz, , compiler figures out if it's property or field you. if create class exposes field xyz, , later decide replace property xyz, recompiling code refers xyz directly takes. however, if wrote code accesses xyz through reflection api, code need rewritten, because compiler not able grab alter you.

c# static static-members

No comments:

Post a Comment