Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 152529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:38:23+00:00 2026-05-11T09:38:23+00:00

I have a class that contains the following two properties: public int Id {

  • 0

I have a class that contains the following two properties:

public int Id      { get; private set; } public T[] Values  { get; private set; } 

I have made it IEquatable<T> and overriden the object.Equals like this:

public override bool Equals(object obj) {     return Equals(obj as SimpleTableRow<T>); }  public bool Equals(SimpleTableRow<T> other) {     // Check for null     if(ReferenceEquals(other, null))         return false;      // Check for same reference     if(ReferenceEquals(this, other))         return true;      // Check for same Id and same Values     return Id == other.Id && Values.SequenceEqual(other.Values); } 

When having override object.Equals I must also override GetHashCode of course. But what code should I implement? How do I create a hashcode out of a generic array? And how do I combine it with the Id integer?

public override int GetHashCode() {     return // What? } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:38:24+00:00Added an answer on May 11, 2026 at 9:38 am

    Because of the problems raised in this thread, I’m posting another reply showing what happens if you get it wrong… mainly, that you can’t use the array’s GetHashCode(); the correct behaviour is that no warnings are printed when you run it… switch the comments to fix it:

    using System; using System.Collections.Generic; using System.Linq; static class Program {     static void Main()     {         // first and second are logically equivalent         SimpleTableRow<int> first = new SimpleTableRow<int>(1, 2, 3, 4, 5, 6),             second = new SimpleTableRow<int>(1, 2, 3, 4, 5, 6);          if (first.Equals(second) && first.GetHashCode() != second.GetHashCode())         { // proven Equals, but GetHashCode() disagrees             Console.WriteLine('We have a problem');         }         HashSet<SimpleTableRow<int>> set = new HashSet<SimpleTableRow<int>>();         set.Add(first);         set.Add(second);         // which confuses anything that uses hash algorithms         if (set.Count != 1) Console.WriteLine('Yup, very bad indeed');     } } class SimpleTableRow<T> : IEquatable<SimpleTableRow<T>> {      public SimpleTableRow(int id, params T[] values) {         this.Id = id;         this.Values = values;     }     public int Id { get; private set; }     public T[] Values { get; private set; }      public override int GetHashCode() // wrong     {         return Id.GetHashCode() ^ Values.GetHashCode();     }     /*     public override int GetHashCode() // right     {         int hash = Id;         if (Values != null)         {             hash = (hash * 17) + Values.Length;             foreach (T t in Values)             {                 hash *= 17;                 if (t != null) hash = hash + t.GetHashCode();             }         }         return hash;     }     */     public override bool Equals(object obj)     {         return Equals(obj as SimpleTableRow<T>);     }     public bool Equals(SimpleTableRow<T> other)     {         // Check for null         if (ReferenceEquals(other, null))             return false;          // Check for same reference         if (ReferenceEquals(this, other))             return true;          // Check for same Id and same Values         return Id == other.Id && Values.SequenceEqual(other.Values);     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a class that contains properties with private set and protected set
I have the following class called City that contains another class called Detail. public
I have the following code: A disposable class that contains two disposable members. One
I have a class that contains a boolean field like this one: public class
I have a class that contains two sets. They both contain the same key
I have a class that contains objects of two other classes. I need one
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
I have a struct that contains two lists: struct MonthData { public List<DataRow> Frontline;
I have a small class that holds two strings as follows: public class ReportType
I have an entity that is made up of properties from two different tables

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.