"are c# structs thread safe?" Code Answer


no, structures in .net are not intrinsically thread-safe.

however, the copy-by-value semantics that structures have great relevance to this converation.

if you are passing your structures around and assigning them in some way to variables or pass-by-value parameters (no ref or out keywords) then a copy is being used.

of course, this means that any changes made to the copy are not reflected in the original structure, but it's something to be aware of when passing them around.

if you are accessing the structure directly in a manner that doesn't involve copy-by-value semantics (e.g. accessing a static field which is the type of the structure, and as marc gravel points out in his answer, there are many other ways) across multiple threads, then you have to take into account the thread-safety of the instance.

By Cerveser on October 12 2022

Answers related to “are c# structs thread safe?”

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