Skip to content

ImmutableArray<T> struct implements IEquatable<T> interface but does not stick to the rules of properly implementing value semantics #77183

Open
@oliverzick

Description

@oliverzick

Description

The ImmutableArray<T> struct implements the IEquatable<T> interface but does not stick to the rules of implementing value semantics acoording to the IEquatable interface.

When comparing two instance of a ImmutableArray<T> both instance are considered equal only when both instances refer to the same contained array instance. Instead both instances should be considered equal when the contents of the contained collections are equal.

Reproduction Steps

using System.Collections.Immutable;

var collection = Enumerable.Range(1, 5);

var instance1 = collection.ToImmutableArray();
var instance2 = collection.ToImmutableArray();

// Result = False ---> Wrong result for equality in terms of value semantics
var result = instance1.Equals(instance2); 

Console.WriteLine($"Result: {result}"); // Outputs "Result: False"

Expected behavior

using System.Collections.Immutable;

var collection = Enumerable.Range(1, 5);

var instance1 = collection.ToImmutableArray();
var instance2 = collection.ToImmutableArray();

// Result = True ---> Should be true because both instances represent a collection of integers from 1 to 5 which are equal in terms of value semantics defined by IEquatable<T> interface
var result = instance1.Equals(instance2);

Console.WriteLine($"Result: {result}"); // Outputs "Result: True"

Actual behavior

Already described in Description and Reproduction Steps sections.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

The IEquatable<T> interface should by properly implemented according to its specification to ensure value semantics of ImmutableArray<T>. This also includes desired behavior of Equals and GetHashCode methods according to interface specification.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions