Check if the given ranges are equal or not in C#

As programmers, we often encounter situations where we need to compare two ranges in a programming language like C#. Whether we're working on complex algorithms or simple programs, checking if two ranges are equal can be a critical task. This article will discuss the process and methods to compare two given ranges in C#, providing a straightforward solution to this common problem.

Understanding Ranges in C#

Before we proceed to the solution, it's vital to have a firm understanding of what ranges are in the C# programming language. Introduced in C# 8.0, ranges are a new feature that provides a syntax for working with subsets of different types of data like arrays, strings, and spans.

A range can be defined using two dots (..). For example, an expression like 1..4 represents a range that includes indices 1, 2, and 3. The Range type represents start and end indices, not the actual values.

Range Comparison Process Range 1 1..4 Indices: 1,2,3 Range 2 1..4 Indices: 1,2,3 Equal: True

Syntax

Following is the syntax for creating and comparing ranges

Range range1 = 1..4;    // Range from index 1 to 4 (exclusive)
Range range2 = 1..4;    // Another range with same bounds
bool areEqual = range1.Equals(range2);

Using Range.Equals() Method

The simplest way to check if two Range objects are equal is to use the built-in Equals()

Example

using System;

public class Program {
   public static void Main() {
      Range range1 = 1..4;
      Range range2 = 1..4;
      Range range3 = 2..5;

      Console.WriteLine($"Range1: {range1}");
      Console.WriteLine($"Range2: {range2}");
      Console.WriteLine($"Range3: {range3}");

      bool areEqual1 = range1.Equals(range2);
      bool areEqual2 = range1.Equals(range3);

      Console.WriteLine($"Range1 equals Range2? {areEqual1}");
      Console.WriteLine($"Range1 equals Range3? {areEqual2}");
   }
}

The output of the above code is

Range1: 1..4
Range2: 1..4
Range3: 2..5
Range1 equals Range2? True
Range1 equals Range3? False

Comparing Array Ranges (Elements)

When comparing actual array elements within ranges rather than Range objects themselves, you need to extract the elements and compare them. Here's a method to compare array ranges by their content

Example

using System;
using System.Linq;

public class Program {
   public static void Main() {
      int[] data = { 0, 10, 20, 30, 40, 50 };
      
      Range range1 = 1..4;  // indices 1,2,3
      Range range2 = 2..5;  // indices 2,3,4
      
      bool areEqual = AreRangeContentsEqual(data, range1, data, range2);
      
      Console.WriteLine($"Array elements in range {range1}: [{string.Join(", ", data[range1])}]");
      Console.WriteLine($"Array elements in range {range2}: [{string.Join(", ", data[range2])}]");
      Console.WriteLine($"Are range contents equal? {areEqual}");
   }

   public static bool AreRangeContentsEqual<T>(T[] array1, Range range1, T[] array2, Range range2) {
      var slice1 = array1[range1];
      var slice2 = array2[range2];
      
      return slice1.SequenceEqual(slice2);
   }
}

The output of the above code is

Array elements in range 1..4: [10, 20, 30]
Array elements in range 2..5: [20, 30, 40]
Are range contents equal? False

Using Custom Comparison Method

For more complex scenarios, you can create a custom method that compares ranges based on specific criteria

Example

using System;

public class Program {
   public static void Main() {
      int[] numbers = { 5, 10, 15, 20, 25, 30 };
      
      Range range1 = 1..3;  // elements: 10, 15
      Range range2 = 3..5;  // elements: 20, 25
      
      bool equalLength = AreRangeLengthsEqual(range1, range2);
      bool equalSum = AreRangeSumsEqual(numbers, range1, numbers, range2);
      
      Console.WriteLine($"Range1 length: {GetRangeLength(range1)}");
      Console.WriteLine($"Range2 length: {GetRangeLength(range2)}");
      Console.WriteLine($"Equal lengths? {equalLength}");
      
      Console.WriteLine($"Range1 sum: {GetRangeSum(numbers, range1)}");
      Console.WriteLine($"Range2 sum: {GetRangeSum(numbers, range2)}");
      Console.WriteLine($"Equal sums? {equalSum}");
   }

   public static bool AreRangeLengthsEqual(Range range1, Range range2) {
      return GetRangeLength(range1) == GetRangeLength(range2);
   }

   public static bool AreRangeSumsEqual(int[] array1, Range range1, int[] array2, Range range2) {
      return GetRangeSum(array1, range1) == GetRangeSum(array2, range2);
   }

   public static int GetRangeLength(Range range) {
      return range.End.Value - range.Start.Value;
   }

   public static int GetRangeSum(int[] array, Range range) {
      var slice = array[range];
      int sum = 0;
      foreach (int value in slice) {
         sum += value;
      }
      return sum;
   }
}

The output of the above code is

Range1 length: 2
Range2 length: 2
Equal lengths? True
Range1 sum: 25
Range2 sum: 45
Equal sums? False

Comparison Methods

Method Use Case Description
Range.Equals() Comparing Range objects Compares start and end indices directly
SequenceEqual() Comparing array contents Compares actual elements within ranges
Custom methods Specific criteria Length, sum, or other property-based comparison

Conclusion

Understanding and comparing ranges in C# involves different approaches depending on whether you're comparing Range objects themselves or their content. Use Range.Equals() for direct range comparison, SequenceEqual() for content comparison, and custom methods for specific criteria like length or sum comparisons.

Updated on: 2026-03-17T07:04:36+05:30

265 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements