c# - How to compare two string arrays

Compare two String Arrays
The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity.

The following .net c# tutorial code demonstrates how we can compare two String Arrays. That means how we can determine whether two String Arrays elements are the same or not. But there is no built-in method in the Array class to check two Arrays equality. So in this .net c# tutorial code example, we will use the Enumerable SequenceEqual() method to compare two String Array objects.

The Enumerable SequenceEqual() method determines whether two sequences are equal according to an equality comparer. The Enumerable SequenceEqual() method returns a Boolean value. This method returns true if the two source sequences are of equal length and their corresponding elements are equal according to the default equality comparer for their type otherwise it returns false.

So finally, using the Enumerable class SequenceEqual() method the .net c# developers can compare two String Arrays.

<%@ Page Language="C#" AutoEventWireup="true"%>  
<!DOCTYPE html>  
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
        string[] colors = { "red", "blue", "green", "yellow" };
        string[] colors2 = { "crimson", "pink", "hotpink", "darkred" };
        string[] colors3 = { "red", "blue", "green", "yellow" };

        Label1.Text = "Colors: ";
        foreach(string s in colors)
            Label1.Text += s + " | ";
        Label1.Text += "<br />Colors2: ";
        foreach(string s in colors2)
            Label1.Text += s + " | ";
        Label1.Text += "<br />Colors3: ";
        foreach(string s in colors3)
            Label1.Text += s + " | ";
        Boolean result = colors.SequenceEqual(colors2);
        Label1.Text += "<br /><br />Colors = Colors2? " + result.ToString();

        Boolean result2 = colors.SequenceEqual(colors3);
        Label1.Text += "<br /><br />Colors = Colors3? " + result2.ToString();
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# linq example - compare string arrays</title>  
    <form id="form1" runat="server">  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# linq example - compare string arrays
        <hr width="550" align="left" color="LightBlue" />    
        <br /><br />
            Text="compare string arrays"  
More c# examples