Interlocked.Exchange can not be used with generics?

I am writing a general class where I need to use Interlocked.

T test1, test2;
Interlocked.Exchange<T>(ref test1, test2);

This will not compile. So am I forced to use Exchange (Object, Object) instead of using MSDN advice to not use it that way?

+3
source share
1 answer

This will work if your common class has the same limitations Interlocked.Exchange<T>as, namely where T: class.

From the docs for Interlocked.Exchange :

This method only supports reference types.

+10
source

Source: https://habr.com/ru/post/1717378/


All Articles