SequenceEqual方法用于测试集合是否相等。
让我们设置三个字符串数组-
string[] arr1 = { "This", "is", "it" }; string[] arr2 = { "My", "work", "report" }; string[] arr3 = { "This", "is", "it" };
现在,使用SequenceEqual()
方法将第一个数组与第二个数组进行比较-
arr1.SequenceEqual(arr2);
以下是一个例子-
using System; using System.Linq; class Program { static void Main() { string[] arr1 = { "This", "is", "it" }; string[] arr2 = { "My", "work", "report" }; string[] arr3 = { "This", "is", "it" }; bool res1 = arr1.SequenceEqual(arr2); Console.WriteLine(res1); bool res2 = arr1.SequenceEqual(arr3); Console.WriteLine(res2); } }
输出结果
False True