Interlocked.Exchange can't be used with generics?
-
05-07-2019 - |
Question
I'm writing a generic class where I need to use Interlocked.
T test1, test2;
Interlocked.Exchange<T>(ref test1, test2);
This won't compile. So am I forced to use Exchange(Object, Object) instead even tho MSDN advices not to use it that way?
Solution
This will work, provided your generic class has the same constraints as Interlocked.Exchange<T>
, namely, where T: class
.
From the docs for Interlocked.Exchange:
This method only supports reference types
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow