9

Int32 struct doesn't define operator overload method for == operator, so why doesn't the code cause compile time error:

if(1 == null) ... ;
6
  • Wouldn't this comparision cause the Int32 to be boxed then do a reference type to reference type comparison that will of course never be false?
    – ben f.
    May 6, 2011 at 19:06
  • @ben f. I think you mean always be false. May 6, 2011 at 19:12
  • 1
    @Brian it's not a duplicate. This question deals with the lack of operators in the System.Int32 class not comparing value types to null. May 6, 2011 at 19:35
  • @Wesley: Yeah, I can totally understand that interpretation. I assumed (possibly errantly) that the question was focused on the null check. If I did misunderstand the question then I wonder why the OP chose 1==null as opposed to 1==2 or the like as an example. May 6, 2011 at 19:56
  • @Wesley: Still looks like a duplicate to me. Voting to close. May 6, 2011 at 21:02

4 Answers 4

26

Let's take a step back here. The question is confusing and the answers so far are not very clear as to what is going on here.

Shouldn't if(1 == null) cause an error?

No. That is legal, though dumb.

How does the compiler deal with operators like "=="? It does so by applying the overload resolution algorithm.

The first thing we must determine is whether this is a "user defined" equality operator or a "built in" equality operator. The left side is a built-in type. The right side has no type at all. Neither of those are user-defined types. Therefore no user-defined operator will be considered. Only built-in operators will be considered.

Once we know that, the question is "which built-in operators will be considered?" The built in operators are described in section 7.10 of the spec. They are equality operators on int, uint, long, ulong, decimal, float, double, any enum type, bool, char, object, string and any delegate type.

All of the equality operators on value types also have a "lifted" form that takes nullable value types.

We must now determine which of those operators are applicable. To be applicable, there must be an implicit conversion from both sides to the operator's type.

There is no implicit conversion from int to any enum type, bool, string or any delegate type, so those all vanish from consideration.

(There is not an implicit conversion from int to uint, ulong, etc, but since this is a literal one, there is an implicit conversion from 1 to uint, ulong, etc.)

There is no implicit conversion from null to any non-nullable value type, so those all disappear too.

What does that leave? That leaves the operators on object, int?, long?, uint?, ulong?, double?, float?, decimal? and char? the remaining nullable types.

We must now determine which one of those remaining applicable candidates is the unique "best" operator. An operator is better than another operator if its operand type is more specific. "object" is the least specific type, so it is eliminated. Clearly every nullable int can be converted to nullable long, but not every nullable long can be converted to nullable int, so nullable long is less specific than nullable int. So it is eliminated. We continue to eliminate operators in this manner. (In the case of the unsigned types we apply a special rule that says that if int? and uint? are both options then int? wins.)

I will spare you the details; ultimately that process leaves nullable int as the unique best operand type.

Therefore your program is interpreted as if((int?)1 == (int?)null), which clearly is legal, and will always be false.

Int32 struct doesn't define operator overload method for == operator

You are correct. What does that have to do with anything? The compiler is perfectly able to do the analysis without it. I don't understand the relationship you believe this fact has to your question. The fact is about a method that could be defined on a type, the question is about how overload resolution chooses a lifted built-in operator. Those two things are not related because "int" is not a user-defined type.

5
  • @Eric: "All of the equality operators on value types also have a "lifted" form" I know it's a stupid question, but I'll ask it regardless - I assume only equality operators on value types have a lifted form, while equality operators on reference types don't have lifted form of any kind?
    – user702769
    May 6, 2011 at 20:50
  • 4
    @user702769: A lifted form of a value type operator is one that can handle null. The reference equality operators are already able to handle nulls because reference types can always be null. If the type system had been designed to support non-nullable reference types then of course there would have to be lifted operators on reference types as well. But it wasn't. Keep that in mind next time you design a type system: bake in non-nullability of both value and reference types from day one! May 6, 2011 at 20:54
  • So why isn't the same thing (if both sides can upcast to a type, do it) done with the conditional operator? May 6, 2011 at 21:05
  • @BlueRaja: The type to which both sides are converted implicitly is determined by overload resolution. The ?: operator is not overloaded in the first place. May 6, 2011 at 21:08
  • @Eric: I love it when you say "Keep that in mind next time you design a type system: bake in non-nullability of both value and reference types from day one!". Only a few people like you have the skills to do this kind of task, if I am not far off :O
    – Joan Venge
    May 10, 2011 at 20:34
4

It is a value type rather than a reference type. So it will not need operators.

http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx

4
  • Value types can not be null, by virtue of representing a value. A null pointer is simply an integer of value 0. So, you can argue that even the null pointer is not null. May 6, 2011 at 19:03
  • A null pointer is not a null reference. This is C#; he's using references, not pointers. May 6, 2011 at 19:11
  • »So it will not need operators.« Could you elaborate on what you mean by saying that value types not needing operators?
    – user702769
    May 6, 2011 at 19:58
  • 4
    This answer appears to be a complete non sequitur. Can you explain why you believe there to be any connection between int being a value type, and it not needing to define an equality operator? Other value types do need to define equality operators; for instance, a value type representing a complex number would typically have defined an equality operator. May 6, 2011 at 20:51
3

The operators for primitive types (every numeric type except decimal) are defined by the language, not the runtime.

They compile to IL instructions rather than method calls (ceq for ==)

4
  • »The operators for primitive types (every numeric type except decimal) are defined by the language, not the runtime.« if(100 == new Object()) causes an error. So how exactly does language define an == operator which accepts as its operands Int32 and null?
    – user702769
    May 6, 2011 at 19:59
  • 1
    @user: see stackoverflow.com/questions/1972262/…
    – SLaks
    May 6, 2011 at 19:59
  • ups, I've totally missed that link. May I just ask why are operators for primitives, say int, defined by the language and not by Int32?
    – user702769
    May 6, 2011 at 20:23
  • @user: Well, how would you define an operation on a primitive? The code has to, at some point, turn into low-level code. Remember that C# compiles into IL, so at some point your C# functions have to turn into IL. Though you can also "cheat" and use [MethodImpl(MethodImplOptions.InternalCall)].
    – Brian
    May 6, 2011 at 21:05
2

Have a look at MSDN blog. This contains an answer to your question.

Not the answer you're looking for? Browse other questions tagged or ask your own question.