Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phương thức SequenceEqual trong C#

SequenceEqual (namespace System.Linq) kiểm tra hai tập hợp có cùng số lượng, cùng thứ tự và các phần tử tương ứng bằng nhau (theo comparer mặc định).

Ví dụ

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

Kết quả

False
True

Ghi chú: Có overload chấp nhận IEqualityComparer<T> để tùy chỉnh so sánh.