T
Tigger
Hi Shawn,
I would have thought the "is" would be more efficient than the "as"
plus null check?
I guess if normal flow is for it to pass the null check then the
argument is if an "is" plus cast is slower than an "as" plus null
check. Which I would guess to be the case. So in those scenarios it
would be faster to do it your way.
I tend to use the pattern as a kind of switch statement
if (obj is AType)
{
AType aType = (AType)obj;
// do atype special things
}
else if (obj is BType)
{
BType bType = (BType)obj;
// do btype things
}
With this scenario its not the most readable but in my opinion it is
slightly easier to read than using "as"s and null checks.
Of course I try to avoid doing this sort of thing by using interfaces,
inheritance, strong typing, generics etc.
Tigger
I would have thought the "is" would be more efficient than the "as"
plus null check?
I guess if normal flow is for it to pass the null check then the
argument is if an "is" plus cast is slower than an "as" plus null
check. Which I would guess to be the case. So in those scenarios it
would be faster to do it your way.
I tend to use the pattern as a kind of switch statement
if (obj is AType)
{
AType aType = (AType)obj;
// do atype special things
}
else if (obj is BType)
{
BType bType = (BType)obj;
// do btype things
}
With this scenario its not the most readable but in my opinion it is
slightly easier to read than using "as"s and null checks.
Of course I try to avoid doing this sort of thing by using interfaces,
inheritance, strong typing, generics etc.
Tigger