Chapter 9


Listing B.2. ProductSerialNumber

public sealed class ProductSerialNumber {     public ProductSerialNumber(         string productSeries, int model, long id)     {         ProductSeries = productSeries;         Model = model;         Id = id;     }     public readonly string ProductSeries;     public readonly int Model;     public readonly long Id;     public override int GetHashCode()     {         int hashCode = ProductSeries.GetHashCode();         hashCode ^= Model; // Xor (eXclusive OR)         hashCode ^= Id.GetHashCode(); // Xor (eXclusive OR)         return hashCode;     }     public override bool Equals(object obj)     {         if (obj == null)         {             return false;         }         if (ReferenceEquals(this, obj))         {             return true;         }         if (this.GetType() != obj.GetType())         {             return false;         }         return Equals((ProductSerialNumber)obj);     }     public bool Equals(ProductSerialNumber obj)     {         // STEP 3: Possibly check for equivalent hash codes         // if (this.GetHashCode() != obj.GetHashCode())         // {         //    return false;         // }         // STEP 4: Check base.Equals if base overrides Equals()         // System.Diagnostics.Debug.Assert(         //     base.GetType() != typeof(object));         // if ( base.Equals(obj) )         // {         //    return false;         // }         // STEP 1: Check for null         return ((obj != null)             // STEP 5: Compare identifying fields for equality.             && (ProductSeries == obj.ProductSeries) &&             (Model == obj.Model) &&             (Id == obj.Id));     }     public static bool operator ==(         ProductSerialNumber leftHandSide,         ProductSerialNumber rightHandSide)     {         // Check if leftHandSide is null.         // (operator== would be recursive)      if (ReferenceEquals(leftHandSide, null))      {          // Return true if rightHandSide is also null          // but false otherwise.          return ReferenceEquals(rightHandSide, null);      }      return (leftHandSide.Equals(rightHandSide));   }   public static bool operator !=(       ProductSerialNumber leftHandSide,       ProductSerialNumber rightHandSide)   {       return !(leftHandSide == rightHandSide);   } }




Essential C# 2.0
Essential C# 2.0
ISBN: 0321150775
EAN: 2147483647
Year: 2007
Pages: 185

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net