How to apply InterLocked.Exchange for Enum Types in C#?

前端 未结 5 998
难免孤独
难免孤独 2021-02-12 22:39
public enum MyEnum{Value1, Value2}  
class MyClass 
{ 
    private MyEnum _field;   
    public MyEnum Field  // added for convenience
    {
        get { return _field;         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-12 23:13

    You appear to not need the "exchange" feature of Interlocked.Exchange, as you are ignoring its return value. Therefore I think the solution that might make you happiest is to mark _field as volatile:

    private volatile MyEnum _field;
    

提交回复
热议问题