The following code works properly in .NET core 3.1, but generates wrongly the warning CS8625 Cannot convert null literal to non-nullable reference type:
#nullable enable
using System.Threading;
namespace InterlockedExchangeNullProblem {
public class Class1 {
public Class1() {
object? o = new object();
var o1 = Interlocked.Exchange(ref o, null); // ok
class2? c = new class2();
var c1 = Interlocked.Exchange(ref c, null); // error CS8625 Cannot convert null literal to non-nullable reference type.
}
}
public class class2{}
}
If it works for object?, it should also work for class2?.