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);
}