Is vs. As Performance

posted on 10/08/07 at 12:34:22 am by Joel Ross

Sometimes it has to be completely laid out for me to finally "get it". That's what happened tonight when I was reading Adel Khalil's blog post about the performance of "is" vs. "as".

When you read the post, it's obvious what's happening, but when I've used "is" in the past, I've never really thought about it. I think the reason I've picked one over the other is because you're taught the proper way to develop is to check things before you use them. If you use an object, check it's not null. If you get parameters, check them before you use them.

So it seemed natural that if you are casting from one type to another, you check that it's of that type, right? Not in this case - it's more performant (and just as safe) to do the check for null after you try the cast.

I'll have to remember that in the future.

Categories: Development, C#