Why does the is operator return false when given null?

后端 未结 7 1820
小蘑菇
小蘑菇 2020-11-29 23:59

It seems to me that the is operator is a bit inconsistent.

bool Test()
{
    // Returns false, but should return true.
    return null is string         


        
7条回答
  •  抹茶落季
    2020-11-30 00:53

    http://msdn.microsoft.com/en-us/library/scekt9xw%28v=vs.71%29.aspx

    An is expression evaluates to true if both of the following conditions are met:

    • expression is not null.
    • expression can be cast to type. That is, a cast expression of the form (type (expression) will complete without throwing an exception. For more information, see 7.6.6 Cast expressions.

提交回复
热议问题