What type should the overload of the negation operator (!) Have?

If I overload the operator ! in class, what type should it return? In the book, I found this (partial listing):

 public class MyType { public int IntField { get; set; } public MyType(int intField) { IntField = intField; } public static bool operator !(MyType mt) { return (mt.IntField <= 0); } 

It compiles, but I expect a statement ! will return an instance of MyType , something like

 public static MyType operator !(MyType mt) { var result = new MyType(-mt.IntField); return result; } 

In fact, I expect the compiler to require a statement ! returned MyType . But this is not so.

So ... why the operator return type ! shouldn't be a content type? You need to make the type of the return type ++ or -- be the type of the containing one.

+4
source share
1 answer

Suppose I asked you: "What is the type of return of concatenation"? What would you say? Perhaps you turned around and asked "concatenation of what?". Catenation is determined by characters, strings, sequences, cataclysed requirements, languages, discrete state machines, and thousands of other things, so the return type is determined by the type of arguments. But a typical type of concatenation is the type of arguments. Not always; For example, concatenating two characters is a string. But usually.

Similarly, the question: β€œWhat type of operator!”? completely depends on what is denied, and you did not say that you deny. Usually the negation of T is another T, but this is not necessary.

I suspect you are not asking the right question. I think the question you should ask is "a realistic scenario in which you would overload the operator!" The example you give from the book is terrible; this does not motivate why the author of the code generally redefines the operator.

Here is a more realistic example. Suppose we lived in a world without null types. You can decide to have a three-digit logic:

 sealed class MyBool { private MyBool() {} // No one creates it! public static MyBool True = new MyBool(); public static MyBool False = new MyBool(); public static MyBool Unknown = new MyBool(); 

Ok, what is the rule for denying MyBool? True becomes False, False becomes True, Unknown remains Unknown:

  public static MyBool operator !(MyBool b) { if (b == True) return False; if (b == False) return True; return Unknown; } 

In this case, type! The operator is MyBool.

Of course, since C # 2.0 has three-valued logic in the form of Nullable<bool> , but you may need more complex types of logic (or you can write C # 1.0 code).

It is more difficult to come up with reasonable examples of situations in which denial of Foo leads to a bar; something like β€œmonadic” workflow objects may be a possible situation - where the negation of an object of this type is an object of another type that represents the delayed execution of the negation.

+11
source

All Articles