C# “is” operator - is that reflection?

前端 未结 3 449
深忆病人
深忆病人 2020-12-29 02:42

A colleague asked me an interesting question today - is the C# keyword/operator \"is\" considered reflection?

object tmp = \"a string\";
if(tmp is String)
{
         


        
3条回答
  •  长发绾君心
    2020-12-29 03:48

    The is operator essentially determines if a cast is possible, but instead of throwing an exception when the cast is impossible it returns false. If you consider casting reflection then this is also reflection.

    EDIT:

    After some research I have discovered that a cast is performed in IL på the castclass instruction while the is operator maps to the isinst instruction. FxCop has a rule that warns you if you are doing unecessary casts by first using the isinst and then the castclass instruction. Even though the operations are efficient they still have a performance cost.

提交回复
热议问题