4

I using Resharper 8 and when I wrote

if (sender is Button) 

Resharper reclaim to use as keyword and change it to:

Button button = sender as Button;
if (button != null)

There’s any particular reason?

are using as is real better then is? or in this case as is better?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Akrem
  • 5,033
  • 8
  • 37
  • 64
  • 1
    I assume (i don't have resharper) that it's because you want to use the `Button` later. The `as` casts it and checks whether or not it could be casted. The `is` just checks it, you have to cast it anyway. – Tim Schmelter May 28 '14 at 11:16

4 Answers4

4

as is arguably better if the resulting expression is used later on; is is just as good if not. E.g.

if (sender is Button) {
    Button button = (Button)sender;  // just use `as`, as suggested
    button.Push();

(I thought ReSharper only gave a warning/hint in the case where a duplicate cast could be eliminated, such as this..)

That being said, I use the structure

Button button;
if ((button = sender as Button) != null) {
    button.Push();

to ensure that the variable is initialized in (and only starting in) the condition - this defers the application of as to the appropriate conditional expression and allows C#/ReSharper to detect some incorrect [uninitialized] variable usage cases.


See also:

Casting vs using the 'as' keyword in the CLR - Jon's answer has 'is/(cast)' pairings as a "don't do" raising concerns with both shared and field vs. property access. Performance and 'is' vs 'as' differences are also discussed.

And a duplicate Which code is better: using "as" or "is"?, with some better related links.

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
  • 2
    Also note that in C# 6 we will be able to move the declaration of `Button` inside the `if` itself, improving it still further: `if ((Button button = sender as Button) != null) {` – Matthew Watson May 28 '14 at 11:48
3

I think it's because the as keyword does both, type check and cast at once. So if you later using the sender, you don't have to cast it

AcidJunkie
  • 1,878
  • 18
  • 21
  • That pretty much is it - albei the JIT, or even the compiler, COULD optimize this.... it is not done to my knowledge, so he AS form saves one cast. – TomTom May 28 '14 at 11:21
3

The "as" operator is used to perform conversions between compatible types. Whereas the "is" operator is used to check whether the run-time type of an object is compatible with a given type or not.

So I think as would be a better option to go with as the as operator attempts to cast an object to a specific type, and returns null if it fails.

You can also check Eric Liperts blog on Is is as or is as is?

However, in practice the CLR provides us instruction isinst, which ironically acts like as. Therefore we have an instruction which implements the semantics of as pretty well, from which we can build an implementation of is. In short, de jure is is is, and as is as is is, but de facto is is as and as is isinst.

And What's the difference between "as" and "cast" operators?

From here:

Advantage of 'as' over 'is

In the case of is operator, to type cast, we need to do two steps:

  1. Check the Type using is
  2. If it’s true then Type cast

Actually this affects the performance since each and every time the CLR will walk the inheritance hierarchy, checking each base type against the specified type. To avoid this, use as it will do it in one step. Only for checking the type should we use the is operator.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

You did not post all your code. I guess in your code, if the sender is a button, you cast it to button and do something with it. If that's the case, you did that work twice, once in the if and once in the statement block. Resharper told you to do it once and be done with it.

nvoigt
  • 75,013
  • 26
  • 93
  • 142