Ross Coded Classes

Programming tips with no rose colored glasses.

Pop quiz #1

What could be wrong with this class?  Think carefully and post your answer in the comments. 


class Something
      public string ServiceName { get; private set; }
      public int DbId { get; private set; } 

      public Something(string serviceName, int dbId)
            if (serviceName == null) throw new ArgumentNullException("serviceName");
            ServiceName = serviceName;
            DbId = dbId;

      public override int GetHashCode()
            return ServiceName.GetHashCode() + DbId;

      public override bool Equals(object obj)
            Something other = obj as Something;
            return other != null && DbId == other.DbId && ServiceName == other.ServiceName;

2 thoughts

  1. Having made that mistake … possibility of overflow in GetHashCode().

    Hey, was this adapted from my code? 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.