0

In python isinstance(foo, (Bar,)) is considered slow because Python need to check for the object hierarchy to see if the condition hold. Does this happen on C# too?

I'm particularly interested if (maybeNullFoo is Foo foo) is slower than (maybeNullFoo != null) in C#? I'm using this in the codebase to cancel a nullable variable, using is Foo foo the compiler knows that foo is not null so I don't need to use ! or ?

geckos
  • 4,912
  • 1
  • 34
  • 40
  • 1
    those two statements do different things. My guess is that `maybeNullFoo != null` would be insignificantly faster, since it only checks for `null` and doesn't create a new object from a cast of the original. But if you're doing that new object creation in the body of the `if` statement, then they're likely the same. Either way, it's not likely going to be a noticeable perf difference. – Rufus L Aug 13 '21 at 21:40

0 Answers0