ValueType.Equals
using System;
public struct Complex {
public double re, im;
public override bool Equals(Object obj) {
return obj is Complex && this == (Complex)obj;
}
public override int GetHashCode() {
return re.GetHashCode() ^ im.GetHashCode();
}
public static bool operator ==(Complex x, Complex y) {
return x.re == y.re && x.im == y.im;
}
public static bool operator !=(Complex x, Complex y) {
return !(x == y);
}
}
class MyClass {
public static void Main() {
Complex cmplx1, cmplx2;
cmplx1.re = 4.0;
cmplx1.im = 1.0;
cmplx2.re = 2.0;
cmplx2.im = 1.0;
if (cmplx1 != cmplx2)
Console.WriteLine("The two objects are not equal.");
if (! cmplx1.Equals(cmplx2))
Console.WriteLine("The two objects are not equal.");
cmplx2.re = 4.0;
if (cmplx1 == cmplx2)
Console.WriteLine("The two objects are now equal!");
if (cmplx1.Equals(cmplx2))
Console.WriteLine("The two objects are now equal!");
}
}
Related examples in the same category