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)...

[667 byte] By [ChrisNahr] at [2007-12-16]
# 1
I've filed a bug with the MSDN Product Feedback Center. Please read the description, see the sample, and cast your vote under ID FDBK32494, or go directly to this link: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=d1259993-e550-40e2-9ad0-0041653e5fcf
ChrisNahr at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# Language...
# 2
You need to constrain T to be a class. This compiles

class testClass
{
public int TestFunc<T>(T x) where T:class
{
if (x == null) return 0;
return 1;
}
}

mpswaim at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# Language...
# 3
mpswaim wrote:
You need to constrain T to be a class.

Uhmm, from the original post...

After all, the documentation explicitly states that null comparisons are possible (and will return false if typeof(x) is a value type).
dkocur2 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# Language...
# 4
Yeah, and this is for generic collection classes, so putting any constraints on the type parameters is not an option.
ChrisNahr at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# Language...
# 5
OK, I've upped my vote, and added the section of the of the standard that this violates to the comments in the bug report.
mpswaim at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# Language...
# 6
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.
mpswaim at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# Language...
# 7

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.

ChrisNahr at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# Language...
# 8
Update: Microsoft just closed the bug report and said the issue will be fixed for the final release, possibly earlier. Thanks!
ChrisNahr at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# Language...