1、string.Equals()
1 2 3 4 5 6 7 8 9 10 11 12
| string[] strList1= new string[3] {"1", "2", "3"}; string[] strList2= new string[3] {"4", "5", "6"}; if (!string.Equals(strList1, strList2)) { }
if (!string.Equals(JsonConvert.SerializeObject(list1), JsonConvert.SerializeObject(list2))) { }
|
2、通过使用相同类型的默认相等比较器对序列元素进行比较,以确定两个序列是否相等。
1 2 3 4
| if (Enumerable.SequenceEqual(strList1,strList2)) { }
|
3、数组逐个值比较
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public static bool CompareArray(string[] list1, string[] list2) { var length1 = list1.Length; var length2 = list2.Length; if (length1 != length2) return false; for (var i = 0; i < length1; i++) { if (list1[i] != list2[i]) return false; } return true; }
var isEqual = CompareArray(strList1, strList2); Console.WriteLine("是否相等:" + isEqual);
|
4、数组的交集
1 2 3 4 5 6 7 8 9 10 11 12 13
| var sameArr = arr1.Intersect(arr2).ToArray();
var diffArr = arr1.Where(c => !arr2.Contains(c)).ToArray(); if (diffArr.Count() == 0) { } else { }
|
4、string.Compare()
5、StringComparer类