Skip to content

EquatableArray

A wrapper around an array that implements IEquatable<T> for value-based equality comparison.

C# Signature
internal readonly struct EquatableArray<T> : System.IEquatable<EquatableArray<T>>, System.Collections.Generic.IEnumerable<T> where T : System.IEquatable<T>

The following example uses EquatableArray in a model passed through an incremental generator pipeline so that changes are detected by value instead of by reference.

C#
using System.Collections.Immutable;
using Hertzole.SourceGen;
internal readonly record struct TypeModel(string Name, EquatableArray<string> Members);
internal static class ModelFactory
{
public static TypeModel Create(string name, ImmutableArray<string> members)
{
// Implicitly converts ImmutableArray<T> to EquatableArray<T>.
return new TypeModel(name, members);
}
public static bool HasSameMembers(TypeModel left, TypeModel right)
{
// Compares the contents of the arrays, not the array references.
return left.Members.Equals(right.Members);
}
}

Creates a new EquatableArray<T> from the specified array. The array is copied.

C# Signature
public EquatableArray(T[] array);

The array to wrap.

Creates a new EquatableArray<T> from the specified immutable array.

C# Signature
public EquatableArray(ImmutableArray<T> array);

The immutable array to wrap.

C# Signature
public bool IsEmpty { get; }
C# Signature
public int Length { get; }
C# Signature
public ref readonly T this[int index] { get; }

Converts this EquatableArray<T> to an ImmutableArray<T>.

C# Signature
public ImmutableArray<T> AsImmutableArray();

An ImmutableArray<T> containing the elements.

Returns a read-only span over the elements of this EquatableArray<T>.

C# Signature
public ReadOnlySpan<T> AsSpan();

A read-only span over the elements.

Determines whether this instance is equal to another EquatableArray<T>.

C# Signature
public bool Equals(EquatableArray<T> other);

The other instance to compare.

true if this instance is equal to other; otherwise false.

Determines whether this instance is equal to the specified object.

C# Signature
public override bool Equals(object? other);

The object to compare.

true if this instance is equal to other; otherwise false.

Returns an enumerator that iterates through the elements.

C# Signature
public ImmutableArray<T>.Enumerator GetEnumerator();

An enumerator that iterates through the elements.

Returns the hash code for this instance.

C# Signature
public override int GetHashCode();

The hash code for this instance.

Implicit (ImmutableArray<T> to EquatableArray<T>)

Section titled “Implicit (ImmutableArray<T> to EquatableArray<T>)”

Implicitly converts an ImmutableArray<T> to an EquatableArray<T>.

C# Signature
public static implicit operator EquatableArray<T>(ImmutableArray<T> array);

The converted EquatableArray<T>.

Implicit (EquatableArray<T> to ImmutableArray<T>)

Section titled “Implicit (EquatableArray<T> to ImmutableArray<T>)”

Implicitly converts an EquatableArray<T> to an ImmutableArray<T>.

C# Signature
public static implicit operator ImmutableArray<T>(EquatableArray<T> array);

The converted ImmutableArray<T>.

Equality(EquatableArray<T>, EquatableArray<T>)

Section titled “Equality(EquatableArray<T>, EquatableArray<T>)”

Determines whether two EquatableArray<T> instances are equal.

C# Signature
public static bool operator ==(EquatableArray<T> left, EquatableArray<T> right);

true if left is equal to right; otherwise false.

Inequality(EquatableArray<T>, EquatableArray<T>)

Section titled “Inequality(EquatableArray<T>, EquatableArray<T>)”

Determines whether two EquatableArray<T> instances are not equal.

C# Signature
public static bool operator !=(EquatableArray<T> left, EquatableArray<T> right);

true if left is not equal to right; otherwise false.