"is a readonly field in c# thread safe?" Code Answer


yes - your code doesn't expose this within either constructor, so no other code can "see" the object before it's been fully constructed. the .net memory model (as of .net 2) includes a write barrier at the end of every constructor (iirc - search joe duffy's blog posts for more details) so there's no risk of another thread seeing a "stale" value, as far as i'm aware.

i'd personally still usually use a property instead, as a way of separating implementation from api, but from a thread-safety point of view it's fine.

By dsgdfg on January 24 2022

Answers related to “is a readonly field in c# thread safe?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.