Why does the 'as' keyword work while the () cast does not.

Posted by Mausimo on Stack Overflow See other posts from Stack Overflow or by Mausimo
Published on 2010-04-30T19:43:57Z Indexed on 2010/04/30 19:47 UTC
Read the original article Hit count: 148

Filed under:
|
//always works, returning a valid object into _page
        _page = _httpContext.Handler as System.Web.UI.Page;

//Fails throwing the exception : Unable to cast object of type 'System.Web.DefaultHttpHandler' to type 'System.Web.UI.Page'
        _page = (System.Web.UI.Page)_httpContext.Handler;

I would like to know why this happens?

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET