Is this allowed? If not, can this be accomplished inherently by overloading the implicit/explicit conversion operators?
Asked
Active
Viewed 293 times
4
-
5I wouldn't want to pick up the code that made use of such a feature. Leave this nonsense where it belongs, with C/C++. What are you trying to solve? – spender Jan 20 '11 at 14:55
-
Just wondering about possible implications for casting between base & derived types. I figured I'd ask because someone on SO might know and because questions based around the words `as` and `is` are incredibly hard to Google. – Joel B Jan 20 '11 at 17:45
3 Answers
15
Is overloading the "is" and "as" operators allowed?
No.
If not, can this be accomplished inherently by overloading the implicit/explicit conversion operators?
No. The "as" and "is" operators tell you what a thing really is. They pay no attention to user-defined conversions.
See
http://blogs.msdn.com/b/ericlippert/archive/2010/09/16/is-is-as-or-is-as-is.aspx
for some more thoughts on the "as" and "is" operators.

Eric Lippert
- 647,829
- 179
- 1,238
- 2,067
-
And why can't we overload them? Was this a design decision, or is it stupid enough that no-one really thought of that? I've never needed to overload `is`, but I've never needed to overload '>>' either. – SWeko Jan 20 '11 at 15:08
-
2@SWeko: We don't need to provide reasons to *not* implement a feature. If there are no reasons *to* implement a feature, or the reasons do not justify the costs, then the feature doesn't get implemented. What's the compelling benefit of the proposed feature? (The benefit of overloading the shift operator is that you can define your own bitarray type that has a bit shift operator on it.) – Eric Lippert Jan 20 '11 at 15:11
-
I'm just fishing for apocrypha :) That specific feature would be worse than useless, but was it ever considered? P.S. If I wanted to lobby for a feature, I'd lobby for extension properties, not `is` overloading... – SWeko Jan 20 '11 at 15:15
-
0
No, because they are not operators as such, they are keywords of the language.
Also, I'm not sure what you would overload them to and still preserve the semantics of the language spec? Seems like it could be used only for great evil :)

Phill
- 1,302
- 1
- 9
- 18
-
6They most certainly are operators. They're binary operators that take an expression on one side and a type on the other. – Eric Lippert Jan 20 '11 at 14:53
-
Hi Eric, yes, I realised after I posted that they are of course operators. Lack of coffee today. – Phill Jan 20 '11 at 14:55