1

How does the "is" operator work in C#?

I have been told that this :

if (x is string)
{
     string y = x as string;
     //Do something
}

is not as good as this:

string y = x as string;
if (y != null)
{
     //Do something
}

Which is better and why?

David Pilkington
  • 13,528
  • 3
  • 41
  • 73
  • 6
    They're actually opposite, unless you meant `if (y != null)` instead of `if (y == null)`. – Frédéric Hamidi Oct 31 '13 at 10:39
  • See the MSDN documentation on how the `is` operator works: http://msdn.microsoft.com/en-us/library/scekt9xw(v=vs.120).aspx – Qantas 94 Heavy Oct 31 '13 at 10:39
  • That's because `is` and `as` are doing the same operation, so you're doing it twice in the first snippet. – Patrick Oct 31 '13 at 10:39
  • 3
    Why do you use `as` at all in the first approach? You have checked already that it's a string, so cast it directly. – Tim Schmelter Oct 31 '13 at 10:39
  • @Patrick: This question is *not* about `(type)identifier` casting vs. `identifier as type` casting. If anything, it is about `identifier as type` casting vs. checking with `is` + casting. – O. R. Mapper Oct 31 '13 at 10:46
  • If you have checked with `is`, the first code should just use `string y = (string)x;` - no need for `as` – Marc Gravell Oct 31 '13 at 10:57

3 Answers3

2

FxCop issues Warning CA1800 in the first scenario (and not only when using as, but also when using an unchecked cast) as both is and the actual casts require certain type checking operations to determine whether the cast is successful or whether to throw an InvalidCastException.

You might save a few operations by just using as once and then checking the result for null if you are going to use the cast value anyway, rather than checking explicitly with is and then casting anew.

Community
  • 1
  • 1
O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114
1

I think second is better cause in first case it will cast object 2 times, first time with is operator and second time in as operator.

while in second case it cast only one time.

The is operator checks if an object can be cast to a specific type or not

like

    if (someObj is StringBuilder)
{
StringBuilder ss = someObj as StringBuilder
....
}

The as operator cast an object to a specific type, and returns null if it fails.

like

StringBuilder b = someObj as StringBuilder;
if (b != null) ...
Upendra Chaudhari
  • 6,473
  • 5
  • 25
  • 42
0

I would use the first approach when more than one type is expected to be stored in x. (You might be passed a string, or a StringBuilder etc). This would allow for non-exception based handling.

If you are always expecting x to hold a certain type of value, then I would use the second option. The check for null is inexpensive and provides a level of validation.

-- Edit --

Wow. After reading some of the comments below, I started looking for more updated information. There is a LOT more to consider, when using as vs is vs (casting). Here are two interesting reads I found.

Does it make sense to use "as" instead of a cast even if there is no null check? and http://blogs.msdn.com/b/ericlippert/archive/2009/10/08/what-s-the-difference-between-as-and-cast-operators.aspx?PageIndex=1#comments

Both of which seem to be well summarized by Jon Skeet's blog. http://www.yoda.arachsys.com/csharp/faq/#cast.as

Community
  • 1
  • 1
Brad Bruce
  • 7,638
  • 3
  • 39
  • 60
  • The second approach will never throw an exception, either. – O. R. Mapper Oct 31 '13 at 10:57
  • When I investigated this before, I found that the second approach used a try/catch block internally. – Brad Bruce Oct 31 '13 at 11:04
  • Interesting - do you have any references for this? Is this true for all .NET versions? I'm wondering because [Jon Skeet's FAQ](http://www.yoda.arachsys.com/csharp/faq/#cast.as) implies the opposite, especially if the "as operator appears to be slightly faster in v1.0 and v1.1 of Microsoft's CLR compared to casting", which would hardly be possible if `as` somehow comprised of the casting syntax and an additional `try`-`catch`-construct. – O. R. Mapper Oct 31 '13 at 11:18
  • 1
    I think it was in an "Instructor Written" book trying to explain what it did. It wasn't the first thing that I found wrong with his teaching. (I'm still trying to forget some of it ) – Brad Bruce Oct 31 '13 at 12:09