From https://msdn.microsoft.com/en-us/library/bb297966(v=vs.110).aspx
[ComVisibleAttribute(false)]
public static T CompareExchange<T>(
ref T location1,
T value,
T comparand
)
where T : class
and
NullReferenceException The address of location1 is a null pointer.
But when I use a null reference for location1
, I don't get any errors:
class A { }
class Program
{
static void Main(string[] args)
{
A dest = null;
A src = new A();
// If dest is null, then replace with src.
Interlocked.CompareExchange(ref dest, src, null);
}
}
Is it ok to do this? Is there a danger that this will throw NullReferenceException
in later versions of .NET?