1

Possible Duplicate:
C# “as” cast vs classic cast

What is the difference between these two expressions?

  • (ListView)sender
  • sender as ListView

In General, I usually used the exp sender as ListView. But in SO i find that most times users use (ListView)sender.

So, I want to know which one is more efficient.

Or,

If it is the choice of the coder, which one to use[and both works the same way]??

Community
  • 1
  • 1
Writwick
  • 2,133
  • 6
  • 23
  • 54

6 Answers6

11

The difference is that (ListView)sender will throw an exception if sender isn't a ListView, but sender as ListView will not throw an exception and return null instead if the cast is invalid.

TGH
  • 38,769
  • 12
  • 102
  • 135
5

The difference would be that if for some reason sender was not castable to a ListView, (ListView)sender would throw an exception, while sender as ListView will cause the result to be null.

Maciej
  • 2,175
  • 1
  • 18
  • 29
2
var listview = (ListView)sender  // Throws an exception if sender is not listView

and

var listview = sender as ListView  // listview will be assigned to null if sender is not
                                   // a listview
Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42
0

as operator doesn't throw an exception when it fails but instead fills the Left-Hand-Side variable with null.

While (ListView)sender will throw exception if sender is not ListView.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
0

Using (a)b will throw an exception if b is not assignable to a or cannot be converted, and can be used even when a is a non-nullable type (like int). Using b as a will never throw an exception (it returns null if b cannot be assigned to a), but will not convert (e.g. you can do (int?)12.3 but not 12.3 as int?) and will not work if a isn't nullable (e.g. you can do 12 as int? but not 12 as int).

Gabe
  • 84,912
  • 12
  • 139
  • 238
0

sender as ListView is the same as:

sender is ListView ? (ListView)sender : null

There is practically no difference in efficiency. as is safe against invalid casts since it produces a null result instead of throwing an exception, so I always use as and then check for the null condition.

Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
  • 1
    I think the downside to this is that the IS will also do a cast which will result in a double cast – TGH May 23 '12 at 05:40
  • @TGH `is` performs a runtime check to see if it can complete a reference conversion without causing an exception. It doesn't actually perform the conversion. There are many such checks in the CLR as it is a managed environment, so I wouldn't worry about optimizing against it, or consider it a downside. `as` produces cleaner and safer code in most of the cases (I actually don't know of any cases where I would _choose_ to use a direct cast when I _could_ use `as`). – Eren Ersönmez May 23 '12 at 06:31
  • @ErenErsönmez I choose a direct cast when I *want* an exception to be raised in those "unfortunate situations". It's easier to understand an Invalid Cast Exception than a Null Reference Exception that occurs somewhere later on... if it's going to fail, fail fast. –  May 23 '12 at 07:07
  • @pst If I understand you correctly, you use it in cases where you _know_ a direct cast should work -- if it doesn't, then it's a bug, which you want to catch early in your tests. That is a reasonable choice. – Eren Ersönmez May 23 '12 at 07:33