In both C#/Java the operator precedence of is
respectively instanceof
leads to some ugly necessary parenthesis. For example instead of writing if (!bar instanceof Foo)
you have to write if (!(bar instanceof Foo))
.
So why did the language teams decide that !
has a higher operator precedence than is/instanceof? Admittedly in C# you can overwrite operator!
which would lead to a different result in some situations, but those situations seems exceedingly rare (and non-intuitive in any case), while the case of checking if something is not a type or subtype of something is much more likely.
Conspiracy theory:
C# designers don't want you to use
is
operator. Usage of this operator is often a smell of bad OOP design. If you find yourself using it often, it probably means that your class hierarchy is wrong and you need to rely on virtual methods and patterns more heavily. Java designers went even further: they named the operatorinstanceof
to make you cringe every time you use it.This isn't unlikely actually. There're many cases when language and library designers make some features inconvenient to use. Some examples: character encodings in .NET (you should always use Unicode),
goto
in Pascal (you should avoid it) etc. Sometimes it is caused by bad design (like WPF in .NET), but sometimes it's intentional.