Interlocked.Exchange可为空的十进制 [英] Interlocked.Exchange nullable decimal
本文介绍了Interlocked.Exchange可为空的十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想交换两个可为空的十进制值,如下所示:
I want to exchange two nullable decimal values, like this:
o2 = Interlocked.Exchange(ref o1, o2);
类型为十进制?"为了将其用作通用类型或方法"System.Threading.Interlocked.Exchange(ref T,T)"中的参数"T",必须为引用类型.
The type 'decimal?' must be a reference type in order to use it as parameter 'T' in the generic type or method 'System.Threading.Interlocked.Exchange(ref T, T)'.
有没有比这更好的主意了?
Is there better idea than this:
decimal? temp = o1;
o1 = o2;
o2 = temp;
提前谢谢!
推荐答案
两个想法:
- 将其作为
object
进行处理,并投放给消费者 - 创建一个
Box< T>
class其中T:struct
(并使其不可变),然后交换一些Box<十进制>
参考
- treat it as
object
and cast at the consumer - create a
Box<T>
classwhere T:struct
(and make it immutable), and swap someBox<decimal>
references
在两种情况下,使用者都应先复制值的值(不可重复读取;两次读取之间可能会改变).
In both cases, the consumer should take a clone of the value before anything else (no double reads; it may change between reads).
这篇关于Interlocked.Exchange可为空的十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文