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

前端 未结 4 1238
自闭症患者
自闭症患者 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:50

    private void AddMatchFailure(string failureName, TExpected expected, TActual actual)
    {
        _matchFailures.Add(
            String.Format(MatchFailureFormat, failureName,
            (expected == default(TExpected)) ? "null" : expected.ToString(),
            (actual == default(TActual)) ? "null" : actual.ToString()));
    }
    

    Should do it.

    default(T) gives the default value for that type, for reference types that's null - for others it depends. (Enums it's the equivalent of (enumType)0 for example).

提交回复
热议问题