Why don't relational operators return bool as a type?

Why don't relational operators return bool as a type?

  • Comments 1

Section 10.9.2 of the language specification says:

A binary operator must take two parameters, at least one of which must have the class or struct type in which the operator is declared. Parameters of the shift operators (§7.8) are further constrained. A binary operator can return any type.

Why isn't it limited to just returning bool?

There is some weirdness here because of the C# ability to support nullable types, such as the SQL types (ie SQLInt32, SQLSingle, etc.). These type can either have a value or be null.

One of the strange features of the SQL expression syntax is if either operand in a binary operation is null, the result is null. So, when we want to write the comparison operator for SQLInt32, we have a problem if the result type is constrained to bool, since null is neither true or false.

C# therefore allows the comparison operators to return another type as long as the resulting type has operator true and operator false defined. That gives us enough support to be able to write:

SQLInt32 x = ...;

SQLInt32 y = ...;

if (x == y)


    // x and y or equal


else if (x != y)


    // x and y are not equal




    // the result of the comparison is null



Which allows the full richness of SQL types to be expressed.

Author: Eric Gunnerson]

Leave a Comment
  • Please add 6 and 8 and type the answer here:
  • Post
  • It take time to learn this, but once learned it do work for SQL relational operators and any other nullable types relational operators. This behaveour is also defined in the SQL specs. So this worth the time spent and the complexity learned. Well!!!

    If everybody do agree and it's correct so... what's the *damned* reason because the C# team is defining a different behaveour for the relational operators (they called it the "lifting") of the nullable value-types (as int? etc.) in C# 2.0???

Page 1 of 1 (1 items)