ValueType.Equals : System Object Method « Development Class « C# / C Sharp






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

1.illustrates some of the System.Object class methodsillustrates some of the System.Object class methods
2.System Functions: LogOff, Restart, Shutdown, Hibernate, Standby
3.The Point class is derived from System.Object.
4.Object.Equals Method Determines whether the specified Object is equal to the current Object.
5.override Equals method
6.Build Equals method by using the Equals method from member variables
7.Object.GetType Method Gets the Type of the current instance.
8.Object.MemberwiseClone Method Creates a shallow copy of the current Object.
9.Object.ReferenceEquals Method Determines whether the specified Object instances are the same instance.
10.Object.ToString Method Returns a String that represents the current Object.