1.

How to compare two arrays in C#

Answer»

Two arrays can be COMPARED USING the SequenceEqual() method in C#. This method returns TRUE if the two arrays are exactly the same i.e. if they are equal. Otherwise it returns false.

A program that demonstrates the comparison of two arrays using the SequenceEqual() method in C# is given as FOLLOWS:

using System; using System.Linq; namespace Demo {   public class Program   {      public STATIC void Main(string[] args)      {        int[] arr1 = new int[] {9, 5, 1, 7, 3};        int[] arr2 = new int[] {9, 5, 1, 7, 3};        Console.Write("Array 1: ");        foreach(int i in arr1)        {            Console.Write(i + " ");        }        Console.Write("\nArray 2: ");        foreach(int i in arr2)        {            Console.Write(i + " ");        }        Console.Write("\nBoth arrays are equal? " + arr1.SequenceEqual(arr2));      }   } }

The output of the above program is given as follows:

Array 1: 9 5 1 7 3 Array 2: 9 5 1 7 3 Both arrays are equal? True


Discussion

No Comment Found