检查列表在考虑空值时是相等的

Check lists are equal whilst accounting for nulls

public bool Equals(Permissions that)
{
    if (Groups == null && that.Groups != null || Groups != null && that.Groups == null) return false;
    if (Users == null && that.Users != null || Users != null && that.Users == null) return false;

    if (Groups != null && Users != null)
        return Groups.SequenceEqual(that.Groups) && Users.SequenceEqual(that.Users);

    return Groups != null ? Groups.SequenceEqual(that.Groups) : Users.SequenceEqual(that.Users);
}

我如何才能检查两个列表是否相等,同时还以比这更好的方式解释 null?

至少你可以将集合比较提取到单独的方法中:

public static bool BothNullOrEquals<T>(IEnumerable<T> left, IEnumerable<T> right)
{
    if (left == null && right == null)
    {
        return true;
    }
    if (left != null && right != null)
    {
        return left.SequenceEqual(right);
    }
    return false;
}

public bool Equals(Permissions that)
{
     return BothNullOrEquals(Groups, that.Groups) && BothNullOrEquals(Users, that.Users);
}