Question regarding static variables in static classes.
If i have a static class and set the value of a property in it, publically exposed, is the value of this variable set for all instances of the class? So if thread 1 sets the value of property to 999, is the value set also for thread 2 to 999?
Yes, it is. There is only one copy of the static class’ fields inside an AppDomain.
You should however take synchronization into account. If thread 1 sets (writes to) the variable and thread 2 reads it at the same time, you may get unexpected results because it’s possible that one write operation is actually divided into multiple processor instructions.
Suppose you set the value of a
long. This is a 64 bit value and writing it involves at least 2 processor instructions (on a 32-bit machine). Theoretically it’s possible that a read of the samelongvariable is scheduled between the two write instructions, leading to unexpected behavior.