Some operators naturally come in pairs. For example, if you can compare two Hour values by using the != operator, you would expect to be able to also compare two Hour values by using the == operator. The C# compiler enforces this very reasonable expectation by insisting that if you define either operator== or operator!=, you must define them both. This neither-or-both rule also applies to the < and > operators and the <= and >= operators. The C# compiler does not write any of these operator partners for you. You must write them all explicitly yourself, regardless of how obvious they might seem. Here are the == and != operators for the Hour struct:
struct Hour { public Hour(int initialValue) { this.value = initialValue; } ... public static bool operator==(Hour lhs, Hour rhs) { return lhs.value == rhs.value; } public static bool operator!=(Hour lhs, Hour rhs) { return lhs.value != rhs.value; } ... private int value; }
The return type from these operators does not actually have to be Boolean. However, you would have to have a very good reason for using some other type or these operators could become very confusing!