There are two alternatives I am considering as guard clauses to only allow some code to run if I have an object of proper type:
var targetObject = eventArgs.Value as MagicType;
if (targetObject != null)
{
DoStuffWith(targetObject);
}
or else:
if (targetObject is MagicType)
{
// DoStuffWith(targetObject); // not quite. Actually:
DoStuffWith((MagicType)targetObject);
}
Looking at both alternatives, they seem pretty equivalent, but I know the operations (as
vs is
) are conceptually different.
So the question is: which one should be preferred? Is that choice dependent on scenario? And why?