Why does Interlocked.Exchange not support Boolean type?

前端 未结 3 1750
醉话见心
醉话见心 2021-02-03 16:33

Is there some practical reason why the .NET team decided not to support Boolean in Interlocked.Exchange operation?

One of the usage examples is when you want to guarantee

3条回答
  •  别跟我提以往
    2021-02-03 17:33

    if you need a simple solution, you can use the object field to set/get boolean value.

        private object _isRemoved;
        public bool isRemoved
        {
            get
            {
                object returnVal = Interlocked.CompareExchange(ref _isRemoved, false, null);
                return returnVal != null && (bool)returnVal;
            }
            set
            {
                Interlocked.Exchange(ref _isRemoved, value);
            }
        }
    

提交回复
热议问题