3

I have the following scenario:

    public class SomeClass { 
       // Have some other data members as well  
       public int i ; 
    } 

    public class TestClass { 

        public bool SomeFunction() { 
             SomeClass a = new SomeClass(); 
             SomeClass b = new SomeClass(); 
             if (a == b) // this is where I am getting compile error
               return true; 
             return false; 
         } 

        public static bool operator==(SomeClass a, SomeClass b) { 
             if (a.i == b.i)
               return true; 
             // compare some other members as well
             return false; 
        } 
    } 

Is this possible to achieve in C#?

Thanks for the help!

2
  • Why you want do it out of class? Commented Sep 11, 2011 at 6:32
  • Because in the actual code the class, in the example above "SomeClass", is someone else's and I am trying to validate it Commented Sep 11, 2011 at 6:34

2 Answers 2

3

No, it's not possible to override an operator from a class that is not involved in the operation.


You can make a class that implements IEualityComparer<SomeClass>, which can be used instead of the standard comparison in some cases, for example in a dictionary:

var x = new Dictionary<SomeClass, string>(new SomeClassEqualityComparer());

If you just want to use the comparison in your own class, you could make it a regular static method instead of overriding an operator:

public static bool SomeClassEqual(SomeClass a, SomeClass b) { 
  if (a.i == b.i) {
    return true;
  }
  // compare some other members as well
  return false; 
}

Usage:

if (SomeClassEqual(a, b))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. That's what I wanted to know. So I guess I will have to write stupid functions to do the comparison. Darn it.
I think you mean IEqualityComparer<T>, not IEquatable<T>. The former can be used to compare two objects of a particular type, while the latter is used to compare some other object of a specific type to the object implementing IEquatable<T>. In other words, an equality comparer is not a party to the equality test, while an equatable is.
1

To begin with, you can't use return true; on a void method.

Second, overriding operators should be applied to the host class. In your case, inside SomeClass rather than inside TestClass.

Third, when you implement == overriding operator, you should also implement != .

Here is your code, revised and working:

public class SomeClass
{
    // Have some other data members as well  
    public int i;

    public static bool operator ==(SomeClass a, SomeClass b)
    {
        if (a.i == b.i)
            return true;
        // compare some other members as well
        return false;
    }

    public static bool operator !=(SomeClass a, SomeClass b)
    {
        return !(a == b);
    }
}

public class TestClass
{

    public bool SomeFunction()
    {
        SomeClass a = new SomeClass();
        SomeClass b = new SomeClass();
        if (a == b) // this is where I am getting compile error
            return true;

        return false;
    }
}

3 Comments

Sorry my bad. This is just a sample code. I will fix it. Thanks!
Yes that's what I wanted to confirm is it possible to override operator outside the host class?
Thanks for the updated code. Actually I know that putting those methods inside the actual class would work. What my question actually was was that whether or not it could work if I try to override them outside the host class. But I guess it is not possible as answered by @Guffa. Thanks for the reply anyways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.