Xaisoft
Xaisoft

Reputation: 46591

What is the difference between casting and using "as" in C#?

If there is a difference, what is the difference between the two ways of doing the following cast?

In this case e is a GridViewRowEventArgs object.

GridView gv = (GridView)e.Row.FindControl("gv"); //first way

GridView gv2 = e.Row.FindControl("gv") as GridView; //second way

Upvotes: 51

Views: 4567

Answers (8)

Jakob
Jakob

Reputation: 1

I might be stating the obvious here, but one thing that you get with the 'as' cast is, that you are guaranteed to end up with an object of the type you requested. This comes in handy in certain situations.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500015

The differences are:

  • If a cast fails, it throws an InvalidCastException.
  • If the as operator fails, it just returns a null reference.
  • You can't use as with non-nullable value types (e.g. you can't do "o as int").
  • The cast operator is also used for unboxing. (as can be used to unbox to a nullable value type.)
  • The cast operator can also perform user-defined conversions.

EDIT: I've written elsewhere about when I feel it's appropriate to use which operator. That might be worth a read...

Upvotes: 88

jonp
jonp

Reputation: 13600

What isn't mentioned in the above answers is intent -- why are you performing the conversion, and (more importantly) what happens on the lines after the conversion?

For example, I've seen code similar to the following a number of times:

if ((foo as SomeType).SomeMethod()) { /* ... */ }

This could be compared to the cast-using version:

if (((SomeType) foo).SomeMethod()) { /* ... */ }

So, which of these is better?

The cast is.

Using as will result in a NullReferenceException if the conversion fails.

Using a cast will result in an InvalidCastException if the conversion fails.

Now tell me, which is a more useful exception for debugging? A NullReferenceException, which could be produced by nearly anything, or an InvalidCastException, which lets you know what actually went wrong?

Thus, only use as if the conversion is actually optional (meaning that there must be a null check before using the variable). Otherwise, use a cast, thus making your intentions more explicit.

Upvotes: 10

Ken Browning
Ken Browning

Reputation: 29091

Apart from the issue which Jon pointed out, the as keyword effectively casts o as SomeClass. If o isn't derived from SomeClass it returns null. Whereas a simple cast would throw an exception.

SomeClass i2 = o as SomeClass;

becomes

SomeClass i2;
if (o is SomeClass)
    i2 = (SomeClass)o;
else
    i2 = null;

Upvotes: 1

Daniel Brückner
Daniel Brückner

Reputation: 59645

The safe cast as

variable as type

does the same as

(variable is type) ? (type)variable : (type)null

and will not work for value types.

Upvotes: 4

Jerod Venema
Jerod Venema

Reputation: 44632

The "as" statement basically makes an attempt to cast the variable, and returns null if it fails rather than throwing an exception. As such, the value to which you're casting must be nullable - a reference type or a nullable primitive. In your example, you'd have to do:

int? i2 = o as int;

or it won't compile.

Upvotes: 2

Ana Betts
Ana Betts

Reputation: 74654

In general, the difference between a static cast and "as", is that the cast will throw an Exception if it fails, whereas "as" will just set the variable to null.

Upvotes: 2

Rashack
Rashack

Reputation: 4665

If you however used a reference type say Table the first one would raise InvalidCastException in case o was not assignable to Table and the second would just return null.

Upvotes: 1

Related Questions