public enum MyEnum{Value1, Value2}
class MyClass
{
private MyEnum _field;
public MyEnum Field // added for convenience
{
get { return _field;
The Interlocked
methods are fine. You could use a plain old lock
, but that seems like overkill here. However, you are going to need to use a guarded read of some kind in the getter otherwise you may run into memory barrier issues. Since you are already using an Interlocked
method in the setter it makes sense to do the same in the getter.
public MyEnum Field // added for convenience
{
get { return (MyEnum)Interlocked.CompareExchange(ref _field, 0, 0); }
set { Interlocked.Exchange(ref _field, (int)value); }
}
You could also get away with marking the field as volatile
if you like.