Generic comparison to null broken in July CTP
Everyone knows that one cannot ordinarily use ==/!= on generic types because these operators might be undefined.
However, up to Whidbey June CTP it was possible to write (x == null) or (x != null) when x was a generic type. After all, the documentation explicitly states that null comparisons are possible (and will return false if typeof(x) is a value type).
Now I've installed the July CTP, and suddenly compilation gives an error on such null comparisons!
Um, how am I supposed to compare variables of unrestricted generic types to null now? Is that a bug in the CTP? Please don't tell me we're actually supposed to write Object.Equals(x, null)...
There's a new post on the bug report saying that the new behavior is according to the current standard. If so, then my work around's probably close to the best that you're going to get. You could go with two internal implementations, one for reference types, and one for value types, and have your class instantiate the appropriate implementation based on which T is.
Thanks for notifying us of the new post. As I said in my reply on the bug report, I don't quite buy it. For one thing, we're talking about two different standards here that are not necessarily identical -- ECMA vs Microsoft. For another thing, ECMA might simply have forgotten to add the MS exception -- I don't see that they specifically disallow this comparison.
I really don't want to create parallel duplicate implementations for reference and value types. If all else fails I'll have to cast to object before comparing to null... that should work, and maybe the C# compiler will optimize away this comparison for value types.