.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

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -