Why the keyword "how" works until () does

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

EDIT:

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

If I debug the keyword expression "how", I never get a null reference (the object is always assigned properly). However, () cast throws an exception if it does not have an if status.

EDIT: After about 15 runs through the class, I was able to get zero. It looks like it took more runs to find zero compared to how quickly the cast () throws an exception.

OLD: when debugging in the "how" statement every time a class launches breakpoint hits, it never matters.

If tt has debugging in the expression '() within the if range, every time a breakpoint falls into the cast, it works correctly. Werid

+7
source share
9 answers

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

It didn’t technically work. If you notice that _page will be null . He simply was not mistaken.

The as operator is used to tell the application "I want you to try to convert it. It may not be, and I know this, so do not make exceptions, I will deal with it."

The conversion () used to indicate the application: "This object will be passed to this type. If it is not, and I need to know about it."

The difference between two ghosts (and when you should use them) is when you think that something is attributed to another type, and when you “know” something, you can use it for another type.

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

+14
source share

From here:

Using the as operator differs from distinguishing in C # in three important ways:

It returns null when the variable that you are trying to convert is not of the requested type or the chain inheritance in it, instead of throwing an exception. It can only be applied to variables of a reference type that are converted to reference types. Using both will not perform custom conversions, such as implicit or explicit conversion of statements that do. Actually there are two completely different operations defined in IL that process these two keywords (castclass and isinst) - this is not just “syntactic sugar” written by C # to get this behavior. The as statement seems to be slightly faster in v1.0 and v1.1 of the Microsoft CLR compared to casting (even in cases where there are no invalid casts that have much lower casting performance due to an exception).

+7
source share

One significant difference between the cast application and the prefix is ​​that the prefix prefix will throw an exception, while cast will just return null.

+4
source share

If you use the 'as' operator, if the conversion fails, it just returns null. If you perform an explicit conversion, it will throw an exception if the conversion fails. In this situation, an “as” decrease of the expected type is expected. An explicit conversion cannot move to the desired type.

As a rule of thumb, you should always do “how” if you really don't need an explicit cast (for example, when you need to convert to value types).

0
source share

Using as TRIES to pass this object to this particular type, but returns null on failure, and does not throw an exception (whereas a cast throws an exception). Are you sure that whoever has the as clause actually returns a nonzero object?

0
source share

Same as @Tejs responding. A failed conversion with as results in an error.

I want to say that he is different from him, although, as a rule, you should always use an explicit cast. I personally got a much better exception where the conversion failed, and not on another page that received (apparently) an exception from the null link.

One good use case for the as operator is that it converts data from a database and does not want to check System.DBNull

 int someint = cmd.ExecuteScalar() as int? ?? 0; 
0
source share

As many have pointed out, the as operator returns null in this case, avoiding the “problem” but not “working”.

This site has a good description of the 3 differences between using as and casting:

http://en.csharp-online.net/CSharp_FAQ:_What_is_the_difference_between_using_a_cast_and_the_as_operator

0
source share

Actually, this is exactly what was supposed to happen. The throw did not work, and the throw is a mistake, because the cast is not valid. By default, as defaults to null if a failure occurs.

0
source share

As other answers have noted, this boils down to the fact that “how” will return null in case of an invalid cast, while an explicit cast will throw an exception .

Other answers have some debate about whether to lean toward one or the other. If you intend to use “how,” you need to deal at some point with the fact that it could be zero. If you intend to use an explicit cast, you will have to deal with a possible exception. Depending on the use case, I lean towards one or another.

I prefer to use a specific tide when I know that the cast will work, but the compiler does not, and throwing exceptions will be fine (because this is an exceptional case). But if there is a legitimate chance that the cast will be invalid, I prefer to use the "how" and test the null value. This avoids the exception, which is uglier and makes debugging more difficult IMO.

0
source share

All Articles