Check if two list have the same items

前端 未结 4 627
既然无缘
既然无缘 2020-12-31 05:44

I have two lists as below, how can I say that they have the same elements. The order is not important.

var list1 = new List {1,2,3};
var list2 = n         


        
4条回答
  •  耶瑟儿~
    2020-12-31 06:21

    That's what sets (e.g., HashSet) are for. Sets have no defined order, and SetEquals verifies whether the set and another collection contain the same elements.

    var set = new HashSet(list1);
    var equals = set.SetEquals(list2);
    

提交回复
热议问题