简体   繁体   中英

Can I overload the conditional operator in C#?

I did go through this and this before actually posting this. But those are for C++. Do the same things apply on C# as well? And the second link I have provided states that conditional operator cannot be overloaded, I didn't see the reason why(assuming the same applies on C# as well). Can someone provide links for further reading, or just clarify the matter?

EDIT:

I'm not onto any actual scenario where I would need to overload this operator, but I was just curious as to why it can't be overloaded. Is it just because there isn't any actual possible situation where such overloading could provide any significant functionality?

The list of overloadable operators is here . Some you can, and some you can't.

This page shows how to overload the ones that can be overloaded.

As for the conditional operator (?:), no, you cannot overload it. It is specific to booleans. What would it mean if you did overload it? It's a shorthand for if/then/else.

I suppose the way you could rephrase the question is: "Why can't I overload if/then/else"?

The real reason, more than likely, probably has to do with the fact that the designers of the language chose not to implement it. More than likely, providing an overload for this operator doesn't provide enough benefit to merit the testing and effort that would go into making it overloadable. In short, it's probably a time/money reason. For every feature added to a computer language, that means time, effort and testing, which has to be balanced against the benefit provided by the feature.

In the end, some things just aren't worth implementing. While I'm sure you can find some benefit to it, the benefit is probably outweighed by the cost of implementation.

No, the ?: cannot be overloaded. It says so specifically in the documentation: http://msdn.microsoft.com/en-us/library/8edha89s%28VS.71%29.aspx

This is for good reason, if you ask me. ?: affects control flow, so overloading it would mean you'd have to verify the types of the variables in your expression to assess the control flow - every time you tried to figure out what a piece of code was doing.

No, you cannot overload the conditional operator.

Section 7.3.2 of the C# Language Specification 4.0 covers operator overloading:

7.3.2 Operator overloading

All unary and binary operators have predefined implementations that are automatically available in any expression. In addition to the predefined implementations, user-defined implementations can be introduced by including operator declarations in classes and structs (§10.10). User-defined operator implementations always take precedence over predefined operator implementations: Only when no applicable user-defined operator implementations exist will the predefined operator implementations be considered, as described in §7.3.3 and §7.3.4.

The overloadable unary operators are:

+ - ! ~ ++ -- true false

Although true and false are not used explicitly in expressions (and therefore are not included in the precedence table in §7.3.1), they are considered operators because they are invoked in several expression contexts: boolean expressions (§7.20) and expressions involving the conditional (§7.14), and conditional logical operators (§7.12).

The overloadable binary operators are:

+ - * / % & | ^ << >> == != > < >= <=

Only the operators listed above can be overloaded. In particular, it is not possible to overload member access, method invocation, or the =, &&, ||, ??, ?:, =>, checked, unchecked, new, typeof, default, as, and is operators

...

You can not overload an operator that only operates on boolean values. Overloading in C# means that you specify a specific behaviour for a Type when it is used in conjunction with an operator.

The only reason I can see for this to be done is to allow some sort of "true-ish" values, like:

1 == true
0 == false
null == false
new string[] {} == false

Is this what you're trying to do?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM