Can I check if a variable can be cast to a specified type?

后端 未结 7 2214
误落风尘
误落风尘 2020-12-15 15:50

I am trying to verify whether a variable that is passed can be converted to a specific type. I have tried the following but can\'t get it to compile so I assume I\'m going

7条回答
  •  佛祖请我去吃肉
    2020-12-15 16:19

    Checkout this link: http://msdn.microsoft.com/en-us/library/scekt9xw(v=vs.71).aspx

    The is operator is used to check whether the run-time type of an object is compatible with a given type. The is operator is used in an expression of the form:

    if (expression is type){
        // do magic trick
    }
    

    Something you can use?

提交回复
热议问题