What should I do about “Possible compare of value type with 'null'”?

前端 未结 4 1254
自闭症患者
自闭症患者 2021-02-04 23:06

While writing this method for a custom NUnit Constraint.

    private void AddMatchFailure(string failureName, TExpected expected, TActu         


        
4条回答
  •  悲哀的现实
    2021-02-04 23:47

    Don't change the code - just ignore the warning. If the type parameter is a non-nullable value type, the comparison will always fail and it'll always call ToString() instead. I don't know whether it's actually JITted away, but I wouldn't be surprised... and this doesn't sound like it's performance-critical code anyway :)

    I'd personally leave the warning "on", but ignore it in this particular case - possibly with a comment.

    I think I came across the same warning a few times when reimplementing LINQ to Objects.

提交回复
热议问题