Check if two list have the same items

前端 未结 4 643
既然无缘
既然无缘 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:26

    Without using linq.

     private static bool AreListsEqual(List list1, List list2)
     {
            var areListsEqual = true;
    
            if (list1.Count != list2.Count)
                return false;
    
            for (var i = 0; i < list1.Count; i++)
            {
                if (list2[i] != list1[i])
                {
                    areListsEqual = false;
                }
            }
    
            return areListsEqual;
     }
    

提交回复
热议问题