1

I just discovered this operator and I was wondering if it is safe to use or not.

I mean, in Java our teachers always told us to avoid instanceof() because it breaks polymorphism and often shows poor coding.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
xenom
  • 377
  • 1
  • 5
  • 15
  • Yes, it's the same thing. It's safe to use, and appropriate sometimes. – Blorgbeard Jul 03 '12 at 08:42
  • Sometimes it's necessary. Just make sure you're not overusing it. – user541686 Jul 03 '12 at 08:43
  • Your question might be a duplicate of http://stackoverflow.com/questions/282459/what-is-the-c-sharp-equivalent-to-javas-instanceof-and-isinstance – HatSoft Jul 03 '12 at 08:44
  • Not exactly the same thing, I knew it was more or less an equivalent but I wanted to know if it was more common (tolerated ?) to use it in C# – xenom Jul 03 '12 at 08:56

3 Answers3

4

It's 'safe'. At least it's safer to use this check, rather than just cast and assume the type! You may prefer to use the as operator, which performs the cast to the required type if appropriate, or returns null otherwise. Just remember to check for null.

But often an explicit type check is an indication that something is not quite right with your model. Sometimes it's unavoidable, but if you're doing this a lot, I would take a step back and reconsider your model.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Yep so it's more or less the same thing that instanceof. "something not quite right with your model" but sometimes unavoidable. Thanks – xenom Jul 03 '12 at 08:51
  • I've had several discussions with people about instanceof/is, and why you should never use it. Interestingly enough I found much more aversion in the Java community. My opinion is: instanceof/is when checking for an interface, like IComparable, is fine in most cases, and even necessary in generic/framework code. It's better than overusing generics, for example. For the most part you should not rely on concrete implementations though, so I'd second guess code that uses instanceof/is to check for a class. – Oliver Schimmer Dec 04 '17 at 20:03
  • On another note, C# uses is and as for pattern matching, which was introduced just lately. So at least Microsoft thinks it's not bad design per se. – Oliver Schimmer Dec 04 '17 at 20:13
2
if (obj is FooObject)
{
    ((FooObject)obj).Foo();
}

Is equivalent to

if (obj instanceof FooObject) {
    ((FooObject)obj).foo();
} 
Mizipzor
  • 51,151
  • 22
  • 97
  • 138
0

With pattern-matching (C# 7+) you can now do:

if(foo is Bar bar) {
    return bar;
}

Here, bar is equivalent to (Bar)foo.

Switch statements are also relevant, oddly enough:

switch (x)
{
    case int i:
        Console.WriteLine ("It's an int!");
        Console.WriteLine ($"The square of {i} is {i * i}");
        break;
    case string s:
        Console.WriteLine ("It's a string");
        Console.WriteLine ($"The length of {s} is {s.Length}");
        break;
    default:
        Console.WriteLine ("I don't know what x is");
        break;
}
Grault
  • 974
  • 12
  • 31