我想交换两个可为 null 的十进制值,如下所示:
o2 = Interlocked.Exchange(ref o1, o2);
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)'.
还有比这更好的主意吗:
decimal? temp = o1;
o1 = o2;
o2 = temp;
提前致谢!
最佳答案
两个想法:
- 将其视为
object
并投向消费者 - 创建一个
Box<T>
类where T:struct
(并使其不可变),并交换一些Box<decimal>
引用资料
在这两种情况下,消费者都应该先克隆该值(不要重复读取;它可能会在两次读取之间发生变化)。
关于c# - Interlocked.Exchange 可空十进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5719770/