Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What does the interface IStructuralComparable do in C#?
The IStructuralComparable interface in C# enables structural comparison of collection objects, meaning it compares elements in sequence rather than using reference equality. This interface was introduced in .NET 4.0 to provide consistent comparison behavior for collections and tuples.
Syntax
Following is the syntax for the IStructuralComparable interface −
public interface IStructuralComparable {
int CompareTo(object other, IComparer comparer);
}
Parameters
The CompareTo method accepts the following parameters −
-
other − The object to compare with the current instance.
-
comparer − An IComparer object that defines the comparison rules for individual elements.
Return Value
The CompareTo method returns an integer value −
-
Less than zero − Current object precedes the other object in sort order.
-
Zero − Current object occurs in the same position as the other object.
-
Greater than zero − Current object follows the other object in sort order.
Using IStructuralComparable with Arrays
Example
using System;
using System.Collections;
class Program {
public static void Main() {
int[] array1 = { 1, 2, 3 };
int[] array2 = { 1, 2, 4 };
int[] array3 = { 1, 2, 3 };
IStructuralComparable sc1 = array1;
int result1 = sc1.CompareTo(array2, Comparer.Default);
int result2 = sc1.CompareTo(array3, Comparer.Default);
Console.WriteLine("array1 compared to array2: " + result1);
Console.WriteLine("array1 compared to array3: " + result2);
Console.WriteLine("\nDetailed comparison:");
if (result1 < 0)
Console.WriteLine("array1 comes before array2");
else if (result1 > 0)
Console.WriteLine("array1 comes after array2");
else
Console.WriteLine("array1 equals array2");
}
}
The output of the above code is −
array1 compared to array2: -1 array1 compared to array3: 0 Detailed comparison: array1 comes before array2
Using IStructuralComparable with Tuples
Example
using System;
using System.Collections;
class Program {
public static void Main() {
var tuple1 = Tuple.Create("Alice", 25, 50000);
var tuple2 = Tuple.Create("Alice", 30, 60000);
var tuple3 = Tuple.Create("Bob", 25, 50000);
IStructuralComparable sc1 = tuple1;
int result1 = sc1.CompareTo(tuple2, Comparer.Default);
int result2 = sc1.CompareTo(tuple3, Comparer.Default);
Console.WriteLine("tuple1 vs tuple2: " + result1);
Console.WriteLine("tuple1 vs tuple3: " + result2);
Console.WriteLine("\nComparison results:");
Console.WriteLine(result1 < 0 ? "tuple1 < tuple2" :
result1 > 0 ? "tuple1 > tuple2" : "tuple1 == tuple2");
Console.WriteLine(result2 < 0 ? "tuple1 < tuple3" :
result2 > 0 ? "tuple1 > tuple3" : "tuple1 == tuple3");
}
}
The output of the above code is −
tuple1 vs tuple2: -1 tuple1 vs tuple3: -1 Comparison results: tuple1 < tuple2 tuple1 < tuple3
Classes That Implement IStructuralComparable
The following .NET classes provide explicit implementations for IStructuralComparable −
-
Array class − Enables element-by-element comparison of arrays.
-
Generic Tuple classes − Tuple<T1>, Tuple<T1,T2>, Tuple<T1,T2,T3>, and so on.
Conclusion
The IStructuralComparable interface enables element-by-element comparison of collections and tuples in C#. It provides a standardized way to compare structured data types by their content rather than reference equality, making it essential for sorting and ordering operations on collections.
