How Does Resharper Know “Expression is always true”?

前端 未结 4 1402
既然无缘
既然无缘 2020-12-11 15:41

Check out the following code:

private void Foo(object bar)
{
   Type type = bar.GetType();

    if (type != null) // Expression is always true
    {   
    }         


        
4条回答
  •  春和景丽
    2020-12-11 16:02

    object.GetType is not virtual, so you cannot yourself implement a version that returns a null value. Therefore, if bar is null, you will get a NullReferenceException and otherwise, type will never by null.

提交回复
热议问题