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
Best Solution
The differences are:
InvalidCastException
.as
operator fails, it just returns a null reference.as
with non-nullable value types (e.g. you can't do "o as int
").as
can be used to unbox to a nullable value type.)EDIT: I've written elsewhere about when I feel it's appropriate to use which operator. That might be worth a read...