tags:

views:

285

answers:

4

I have defined the following interface

public interface IHaveAProblem
{
    string Issue { get; set; }
}

And here is the implementation of the IHaveAProblem

public class SomeProblem : IHaveAProblem
{
    public string Issue { get; set; }

    public override bool Equals(object obj)
    {
        SomeProblem otherObj = obj as SomeProblem;

        if (otherObj == null)
        {
            return false;
        }

        return this.Issue == otherObj.Issue;
    }

    public override int GetHashCode()
    {
        return base.GetHashCode();
    }

    public static bool operator ==(SomeProblem rhs, SomeProblem lhs)
    {
        // Null check
        if (Object.ReferenceEquals(rhs, null) || Object.ReferenceEquals(lhs, null))
        {
            if (Object.ReferenceEquals(rhs, null) && Object.ReferenceEquals(lhs, null))
            {
                // Both are null.  They do equal each other
                return true;
            }

            // Only 1 is null the other is not so they do not equal
            return false;
        }

        return rhs.Equals(lhs);
    }

    public static bool operator !=(SomeProblem rhs, SomeProblem lhs)
    {
        // Null check
        if (Object.ReferenceEquals(rhs, null) || Object.ReferenceEquals(lhs, null))
        {
            if (Object.ReferenceEquals(rhs, null) && Object.ReferenceEquals(lhs, null))
            {
                // Both are null.  They do equal each other
                return false;
            }

            // Only 1 is null the other is not so they do not equal
            return true;
        }

        return !rhs.Equals(lhs);
    }
}

When I use the object I can get the correct results for the == compare.

SomeProblem firstTest = new SomeProblem()
    {
        Issue = "Hello World"
    };

SomeProblem secondTest = new SomeProblem()
    {
        Issue = "Hello World"
    };

// This is true
bool result = firstTest == secondTest;

However when I try to compare the interfaces it is doing a memory compare rather than the operator == on SomeProblem

IHaveAProblem firstProblem = new SomeProblem()
    {
        Issue = "Hello World"
    };

IHaveAProblem secondProblem = new SomeProblem()
    {
        Issue = "Hello World"
    };

Is it possible have the interface use the == on SomeProblem rather than a memory compare?

I know I can do a firstProblem.Equals(secondProblem) and get the proper result, however I am creating a frame work and I will not know how it is used in the end. I thought == would work correctly.

+8  A: 

The operator== is static. You cannot define static methods for interfaces in C#. Also, for all operators at least one of the argument types needs to be of the same type as the class it is defined in, therefore: No operator overloading for interfaces :(

What you CAN do is use an abstract class instead - and define the operator there. Again, the operator may NOT be virtual (since static methods cannot be virtual...)

As noted by Gergely Orosz below, you can exploit a special case for the operator==: The default implementation of the operator== relies on the Equals function provided by Object (see here http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx )

dionadar
+1  A: 

In C# operators are static and overriding static methods is... messy.

Why don't you just simply override the Equals() method that is inherited from object? That's pretty straightforward and you wouldn't have to mess with overriding static methods.

Gergely Orosz
+2  A: 

IIRC (and I could be wrong here), C# interfaces don't allow operator overloading.

But in this case that's okay. The == operator normally maps to reference equality. It sounds like you want value equality, and that means you want to force them to override the .Equals() (and consequently also .GetHashCode()) functions. You do that by having your interface inherit from IEquatable().

Joel Coehoorn
+1  A: 

There's a fairly thorough discussion of this here. The author seems to have had a bit of a lightbulb moment regarding some assumptions that I think you share:

http://stackoverflow.com/questions/728434/operator-overloading-with-interface-based-programming-in-c

Brian MacKay