Click here to Skip to main content
15,895,805 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Unable To Overload != , Error 3 The operator 'ConsoleApplication13.pl.operator !=(ConsoleApplication13.pl, ConsoleApplication13.pl)' requires a matching operator '==' to also be defined C:\Users\htg\documents\visual studio 2013\Projects\ConsoleApplication13\ConsoleApplication13\Program.cs 37 28 ConsoleApplication13 . t

C#
class Program
    {
        static void Main(string[] args)
        {
            pl a ,b,c;
           a= new pl();
            b=new pl();
            a.mark=99;
            b.mark=10;
            c = a+b;
            if (c != b)
                Console.WriteLine("is not equal");
            else
                Console.WriteLine("both are equal");
            Console.WriteLine(c.mark);
           Console.ReadLine();
        }
    }
    class pl
    {
        public int mark;
        public static pl operator+ ( pl a , pl b) // 1. here It Work's Perfectly At + overloading
        {
            pl mm = new pl();
            mm.mark = a.mark + b.mark;
            return mm;

        }
        public static bool operator!= (pl m , pl n) // 2. unable to overload
        {
            if (m.mark != n.mark)
                return true;
            else
                return false;
        }       
    }
Posted

Yes, it is required to implement overloads for both == and != if you implement one of them.

There's a very standard way of doing this, and you should also implement an over-ride for the .Equals method:
C#
public class SomeClass
{
    public string ClassName { set; get; }

    public SomeClass(string name)
    {
        ClassName = name;
    }

    protected bool Equals(SomeClass other)
    {
        return string.Equals(ClassName, other.ClassName);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != this.GetType()) return false;
        return Equals((SomeClass)obj);
    }

    public override int GetHashCode()
    {
        return (ClassName != null ? ClassName.GetHashCode() : 0);
    }

    public static bool operator ==(SomeClass c1, SomeClass c2)
    {
        return c1.ClassName == c2.ClassName;
    }

    public static bool operator !=(SomeClass c1, SomeClass c2)
    {
        return !(c1 == c2);
    }
}
 
Share this answer
 
Comments
Matt T Heffron 4-Dec-15 12:34pm    
+5
This is not a question, but it's not hard to see what you are missing. It is required that you implemented both == and != (or none of them). If you think about it, you may understand that this is a pretty logical rule.

—SA
 
Share this answer
 
Comments
Hitesh Jain 4-Dec-15 2:33am    
You Mean To Say That When overloading != We have to overload == as well in c#
Sergey Alexandrovich Kryukov 4-Dec-15 2:43am    
Of course. Do you understand that it makes perfect sense? And will you accept my answer formally? That's all you need, at the moment.
—SA
Hitesh Jain 4-Dec-15 2:50am    
yes but why we need to overload both != As well as == or vice versa , see when you are overloading (a!=b) , it will give you result that's it than again why we need to check for(a==b) And It Does Not Make Any Sense
Sergey Alexandrovich Kryukov 4-Dec-15 2:57am    
It's not "visa versa". You need both or none, period. And it does a lot of sense. You just did not think about it in different aspects, not yet.

Why? Because it's the rule. Well, in essence, you are formally-logically right, but not having this requirement would only complicate things for both CLR implementation and your users. You can consider this rule as a practical one.
It's naturally to assume that a==b can always be written in the form !(a!=b) and a!=b — in the form !(a==b). Taking out this freedom from your user would be nasty. So this is the requirement.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900