C#
arrays
comparison
programming
tutorial

Easiest way to compare arrays in C

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

In C#, the easiest way to compare array contents is usually SequenceEqual. The reason this question confuses people is that arrays are reference types, so == compares references by default, not element-by-element contents. Once you separate reference equality from content equality, the right tool becomes much clearer.

Why == Is Usually Not What You Want

This compares whether two variables point to the same array object, not whether the elements are equal.

csharp
1using System;
2
3class Program {
4    static void Main() {
5        int[] a = { 1, 2, 3 };
6        int[] b = { 1, 2, 3 };
7
8        Console.WriteLine(a == b);
9    }
10}

This prints False because a and b are different array instances.

The Easiest Content Comparison: SequenceEqual

For element-by-element comparison, SequenceEqual is usually the clearest answer.

csharp
1using System;
2using System.Linq;
3
4class Program {
5    static void Main() {
6        int[] a = { 1, 2, 3 };
7        int[] b = { 1, 2, 3 };
8
9        Console.WriteLine(a.SequenceEqual(b));
10    }
11}

This returns True only if both arrays have the same length and matching elements in the same order.

Why SequenceEqual Is A Good Default

It is easy to read and has the semantics most developers mean when they say “compare arrays.” It also short-circuits on the first mismatch, so you do not pay for scanning the whole array unnecessarily in unequal cases.

For ordinary one-dimensional arrays of comparable elements, it is usually the best answer.

Manual Comparison Is Still Useful Sometimes

If you need custom behavior, write the loop yourself.

csharp
1using System;
2
3class Program {
4    static bool ArraysEqual(int[] a, int[] b) {
5        if (a == null || b == null) return a == b;
6        if (a.Length != b.Length) return false;
7
8        for (int i = 0; i < a.Length; i++) {
9            if (a[i] != b[i]) return false;
10        }
11
12        return true;
13    }
14
15    static void Main() {
16        int[] a = { 1, 2, 3 };
17        int[] b = { 1, 2, 3 };
18        Console.WriteLine(ArraysEqual(a, b));
19    }
20}

This is useful when:

  • you want custom element comparison,
  • you want to avoid LINQ in a constrained environment,
  • you want explicit null handling or diagnostics.

Multidimensional And Nested Arrays Need More Thought

SequenceEqual works naturally for one-dimensional arrays. If you have arrays of arrays or multidimensional arrays, you need to decide what equality means.

For jagged arrays, you often compare each inner array recursively. For multidimensional arrays, flattening or nested loops may be needed.

So the “easy” answer applies best to standard one-dimensional arrays.

Compare References Only When That Is Actually The Question

Sometimes reference equality is the correct question.

csharp
Console.WriteLine(Object.ReferenceEquals(a, b));

That is useful if you want to know whether two variables refer to the exact same array instance. It is just a different question from content comparison.

A Practical Rule Of Thumb

Use this rule:

  • same object question: ReferenceEquals or ==,
  • same contents question: SequenceEqual,
  • custom semantics question: manual loop or custom comparer.

That keeps intent obvious.

Common Pitfalls

  • Using == and expecting it to compare array contents.
  • Forgetting to handle null when writing manual comparison logic.
  • Assuming SequenceEqual automatically solves nested or multidimensional array comparison exactly the way you want.
  • Writing verbose manual loops when a simple content comparison is all you need.
  • Mixing up reference equality and value equality in tests.

Summary

  • In C#, == compares array references, not their elements.
  • 'SequenceEqual is usually the easiest and clearest way to compare array contents.'
  • Manual loops are useful when you need custom semantics or explicit control.
  • Multidimensional and nested arrays need additional thought beyond the simplest case.
  • The right comparison depends on whether you mean “same object” or “same contents.”

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.