2. Casting by means of ‘(T)’ Vs Casting by means of ‘as (T)’ when possibly not castable:
It’s common that software developers use simple ‘(T)’ casting, instead of ‘as (T)’. And usually it doesn’t have any negative influence because casted objects are always castable. Yet, if there is even a very slight probability that an object can be under some circumstances not castable, „as (T)” casting should be used.
The main difference is that with the "classic" ‘(T)’ method, if the cast fails, an exception(InvalidCastException) is thrown. While on the other hand With the ‘as (T)’ method, it results in null, which can be checked for, and avoid an exception being thrown.
Not recommended approach
//‘(T)’
var man = (Man)Human;
var man = (Man)Human;
Recommended approach
//‘as (T)’
var man = Human as Man ;
Previous Next
This blog giving the details of technology. This gives the details about working with the business processes and change the way. Here explains think different and work different then provide the better output. Thanks for this blog.
ReplyDeleteHR Consultancy in Bangalore