Testing for value equality between two interface instances in c#?
Asked Answered
S

2

5

So I have an interface, lets call it IInterface.

public interface IInterface : IEquatable<IInterface>
{
    string Name { get; set; }
    int Number { get; }
    Task<bool> Update();
}

Then I try and implement the interface in Implementation.

    public bool Equals(IInterface other)
    {
        if (other == null) return false;

        return (this.Name.Equals(other.Name) && this.Number.Equals(other.Number));
    }

    public override int GetHashCode()
    {
        return this.Number.GetHashCode();
    }

    public override bool Equals(object obj)
    {
        var other = obj as IInterface ;
        return other != null && Equals(other);
    }

    public static bool operator ==(Implementation left, IInterface right)
    {
        if (ReferenceEquals(left, right)) return true;

        if (ReferenceEquals(left, null)) return false;

        return left.Equals(right);
    }

    public static bool operator !=(Implementation left, IInterface right)
    {
        return !(left == right);
    }

The problem I am running into is in a setter:

    public IInterface MyIntf
    {
        get { return _myIntf; }
        set
        {
            if (_myIntf == value) { return; }
            _myIntf = value;
        }

Intellisense is showing that the equality test there is testing the references only and treating both left and right as objects. I assume this is because there is no operator overload for ==(IInterface left, IInterface right). Of course, I cannot actually implement that function because == requires one of the sides to match the type of the implementing class. How does one properly make sure two interfaces can be checked for equality against each other?

Update

Got it, you cannot implement == for an interface. I will use Equals. Thanks everyone.

Spring answered 24/4, 2016 at 20:57 Comment(0)
L
4

You should explicitly call Equals:

if (_myIntf != null && _myIntf.Equals(value)) { return; }

Implementing IEquatable<T> does not impact the == operator.

Lonee answered 24/4, 2016 at 21:10 Comment(4)
Is it possible to implement an == operator overload for an interface?Spring
A shorter way with the null propagation operator : _myIntf?.Equals(value) ?? falseFurmenty
@Spring No, you can't. See this question: #5066781Lonee
@Fabien Right. I'm not used to C# 6 syntax yet. However, find it kind of a matter of personal taste, anyway.Lonee
M
3

Use Equals instead of ==:

public IInterface MyIntf
{
    get { return _myIntf; }
    set
    {
        if (_myIntf.Equals(value)) { return; }
        _myIntf = value;
    }
}
Millisent answered 24/4, 2016 at 21:10 Comment(3)
You should use the null propagation operator in case of _myIntfis null : _myIntf?.Equals(value) ?? falseFurmenty
@Fabien So if I want to not allow null anywhere, could I actually use the null propagation operator to replace null with a NullObject Pattern?Spring
@Spring I juste pointed that if _myIntf is null it can end in a NullReferenceException.Furmenty

© 2022 - 2024 — McMap. All rights reserved.