.net - Using as operator to cast, even after using is operator to verify class type in C#? -
i understand using as
operator cast object, on explicit cast, more desirable due fact if cast fails, reference variable goes null instead of throwing exception.
however, lets check see type of class object is, that's inside of list, prior using as
operator, so,
drawablegamecomponent drawcomponent; foreach (component in components) { if (component drawablegamecomponent) { drawcomponent = component drawablegamecomponent; // drawcomponent } }
does using as
operator lose benefits checking is
operator first? doing following cast good, because first check class type using is
before attempting cast?
if (component drawablegamecomponent) { ((drawablegamecomponent)componet).visible = true; }
i'm wondering if there underlying piece i'm missing, or if comes down matter of taste pattern use. latter pattern create garbage through explicit cast?
thanks in advance!
better (saves 1 "cast" - compare generated il):
drawablegamecomponent drawcomponent; foreach (component in components) { drawcomponent = component drawablegamecomponent; if (drawcomponent != null) { // drawcomponent } }
Comments
Post a Comment