Ross Coded Classes

June 16, 2008

Pop quiz #1

Filed under: .NET Tips — Tags: , — Ross Bradbury @ 11:37 am

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;
      }
}
 

3 Comments »

  1. I did not copy the comments from the old blog for this post…

    Comment by Ross Bradbury — March 11, 2010 @ 10:59 am

  2. […] Coded Classes Programming tips for myself and others. « Pop quiz #1 Using ReSharper 4.1 to edit C# XML Documentation Comments […]

    Pingback by Pop Quiz #1 Answer « Ross Coded Classes — March 11, 2010 @ 11:03 am

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

    Hey, was this adapted from my code? :)

    Comment by Dan Muller — March 20, 2010 @ 10:36 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply to Dan Muller Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: