Just reading through the MSDN page about new .NET 4.0
feature SpinLock and can not understand idea behind the following statement:
Do not store SpinLock instances in readonly fields.
My feelings that this is somehow related to value-type specifics but not sure how exactly and why. Could anybody bring more light on this point?
The underlying problem is that the C# compiler creates a copy of a readonly
value type field when you call a non-static method on it and executes that method on the copy - because the method could have side effects that change the value of the struct - which is not allowed for readonly
fields.
For further clarification see "Mutating Readonly Structs".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With