equals-operator

How can i override a base classes == operator, so the override gets called

With code like the following public class Task { string Name; public static bool operator ==(Task t1, Task t2) { return t1.Name = t2.Name && t1.GetType() == t2.GetType(); } } public class TaskA : Task { int aThing; public static bool operator ==(TaskA t1, TaskA t2) { return (Task)t1 == (Task)t2 && t1.GetType() == t...

java: Integer equals vs. ==

As of java 1.5, you can pretty much interchange Integer with int in many situations. However, I found a potential defect in my code that surprised me a bit. The following code: Integer cdiCt = ...; Integer cdsCt = ...; ... if (cdiCt != null && cdsCt != null && cdiCt != cdsCt) mismatch = true; appeared to be incorrectly setting m...

In a struct, is it valid to implement operator== via Equals, but not override Equals and GetHashCode?

Is this valid? public struct MyStruct { public int Foo { get; set; } public static bool operator ==(MyStruct a, MyStruct b) { return a.Equals(b); } public static bool operator !=(MyStruct a, MyStruct b) { return !a.Equals(b); } } (I know it's slightly inefficient because Object.Equals uses...

Object.Equals is virtual, but Object.operator== does not use it in C#?

I got hit by a strange "asymmetry" in C# that I do not really understand. See the following code: using System; using System.Diagnostics; namespace EqualsExperiment { class Program { static void Main(string[] args) { object apple = "apple"; object orange = string.Format("{0}{1}", "ap", "pl...

Should you use 'isEqual' or '==' ?

Hey. I saw a couple of questions here on SO, with ansers including the function isEqual: instead of the standard ==. So far, I have only learned to use the ==, so I'm wondering what's better to use, what are the pros and cons of each? When should you use them? Thank you. ...