Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

asp.net

//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?

EDIT:

                //Fixes the problem
            if(_httpContext.Handler is System.Web.UI.Page)
            _page = (System.Web.UI.Page)_httpContext.Handler;

If i debug the 'as' keyword statement, i never get a null reference (object always assigned properly). However the () cast creates exceptions unless it has the if statment.

EDIT: After about 15 runs through the class i was able to get a null. Seems like it took more runs to find a null compared to how fast the () cast would catch an exception.

OLD: When there is a debug at the 'as' statement every time the class runs the break point hits - never null.

When tthere is a debug in the '()' statement within the if, every time the break point hits the cast works properly. Werid

like image 274
Mausimo Avatar asked Apr 30 '10 19:04

Mausimo


People also ask

Should I use as or <> to cast?

The as operator can only be used on reference types, it cannot be overloaded, and it will return null if the operation fails. It will never throw an exception. Casting can be used on any compatible types, it can be overloaded, and it will throw an exception if the operation fails.

What is difference between AS and cast C#?

A cast communicates to the reader "I am certain that this conversion is legal and I am willing to take a runtime exception if I'm wrong"(I am willing to accept such error). The "as" operator communicates "I don't know if this conversion is legal or not;(Let's try once and see what happens how it goes").

What is the use of as keyword in C#?

The as operator is used to perform conversion between compatible reference types or Nullable types. This operator returns the object when they are compatible with the given type and return null if the conversion is not possible instead of raising an exception.

What is the main difference between the is and as operators?

The is operator returns true if the given object is of the same type, whereas the as operator returns the object when they are compatible with the given type. The is operator returns false if the given object is not of the same type, whereas the as operator returns null if the conversion is not possible.

What is direct cast in C#?

DirectCast() is more strict than the C# casting operator. It only allows you to cast when the item being cast already is the type you are casting to. It won't do any conversion. Use DirectCast if you're absolutely positively sure that an object is the specified type and the run-time type of the expression are the same.


1 Answers

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

This didn't technically work. If you'll notice _page will be null. It just didn't throw an error.

The as operator is used to tell the application "I want you to try and convert this. It might not, and I know this, so don't throw an exception. I'll deal with it accordingly."

The () conversion is used to tell the application, "This object will cast to this type. If it doesn't something is wrong, and I need to know about it."

The difference between the two casts (and when you should use them) is when you "think" something is castable to another type and when you "know" something is castable to another type.

Here is an article by Eric Lippert on the subject (changed to his blog not re-feeded): http://blogs.msdn.com/ericlippert/archive/2009/10/08/what-s-the-difference-between-as-and-cast-operators.aspx

like image 152
kemiller2002 Avatar answered Nov 11 '22 09:11

kemiller2002