Advertisement
Advertisement


Casting: (NewType) vs. Object as NewType


Question

What is actually the difference between these two casts?

SomeClass sc = (SomeClass)SomeObject;
SomeClass sc2 = SomeObject as SomeClass;

Normally, shouldn't they both be explicit casts to the specified type?

2020/07/05
1
88
7/5/2020 9:46:58 PM

Accepted Answer

The former will throw an exception if the source type can't be cast to the target type. The latter will result in sc2 being a null reference, but no exception.

[Edit]

My original answer is certainly the most pronounced difference, but as Eric Lippert points out, it's not the only one. Other differences include:

  • You can't use the 'as' operator to cast to a type that doesn't accept 'null' as a value
  • You can't use 'as' to convert things, like numbers to a different representation (float to int, for example).

And finally, using 'as' vs. the cast operator, you're also saying "I'm not sure if this will succeed."

2009/10/09
86
10/9/2009 5:33:21 AM

Also note that you can only use the as keyword with a reference type or a nullable type

ie:

double d = 5.34;
int i = d as int;

will not compile

double d = 5.34;
int i = (int)d;

will compile.

2008/08/05

Typecasting using "as" is of course much faster when the cast fails, as it avoids the expense of throwing an exception.

But it is not faster when the cast succeeds. The graph at http://www.codeproject.com/KB/cs/csharpcasts.aspx is misleading because it doesn't explain what it's measuring.

The bottom line is:

  • If you expect the cast to succeed (i.e. a failure would be exceptional), use a cast.

  • If you don't know if it will succeed, use the "as" operator and test the result for null.

2008/09/18

A difference between the two approaches is that the the first ((SomeClass)obj) may cause a type converter to be called.

2008/11/07

Here is a good way to remember the process that each of them follow that I use when trying to decide which is better for my circumstance.

DateTime i = (DateTime)value;
// is like doing
DateTime i = value is DateTime ? value as DateTime : throw new Exception(...);

and the next should be easy to guess what it does

DateTime i = value as DateTime;

in the first case if the value cannot be cast than an exception is thrown in the second case if the value cannot be cast, i is set to null.

So in the first case a hard stop is made if the cast fails in the second cast a soft stop is made and you might encounter a NullReferenceException later on.

2008/08/05

Well the 'as' operator "helps" you bury your problem way lower because when it is provided an incompatible instance it will return null, maybe you'll pass that to a method which will pass it to another and so on and finally you'll get a NullReferenceException which will make your debugging harder.

Don't abuse it. The direct cast operator is better in 99% of the cases.

2010/10/28

Source: https://stackoverflow.com/questions/2483
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]