How to check if an object is not of a particular type?

前端 未结 7 1714
名媛妹妹
名媛妹妹 2021-01-03 19:38

I want to check if an object is not of a particular type. I know how to check if something is of a particular type:

if (t is TypeA)
{
   ...
}
         


        
7条回答
  •  醉酒成梦
    2021-01-03 19:46

    If you are doing a TypeA x = (TypeA)t; inside the if block then a better way is

    TypeA x = t as TypeA
    if(x != null)
    {
    ...
    }
    

    This causes only one time type checking rather than twice.

提交回复
热议问题