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 7672923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:23:43+00:00 2026-05-31T16:23:43+00:00

I have some classes as below, i have implemented the Equals(Object) method for almost

  • 0

I have some classes as below, i have implemented the Equals(Object) method for almost all of them. But i don’t know how to write GetHashCode() . As far I used these data types as value type in a Dictionary Collection, i think i should override GetHashCode().

1.I don’t know how to implement GetHashCode() with logic of Equals(Object).

2.There are some derived classes, if i override GetHashCode() and Equals(Object) for base class ( Param ), is it still necessary to override it for childs?

class Param
{
    ...
    public Int16 id { get; set; }
    public String name { get; set; }
    ...
    public override bool  Equals(object obj)
    {
        if ( obj is Param){
            Param p = (Param)(obj);
            if (id > 0 && p.id > 0)
                return (id == p.id);
            else if (name != String.Empty && p.name != String.Empty)
                return (name.equals(p.name));
            else
                return object.ReferenceEquals(this, obj);
        }
        return false;
    }
}  
class Item
{
    public int it_code { get; set; }
    public Dictionary<String, Param> paramAr { get; set; }
    ...
    public override bool Equals(Object obj)
    {
        Item im = new Item();
        if (obj is Item)
            im = (Item)obj;
        else 
            return false;

        if (this.it_code != String.Empty && im.it_code != String.Empty)
            if (this.it_code.Equals(im.it_code)) 
                return true;

        bool reParams = true;
        foreach ( KeyValuePair<String,Param> kvp in paramAr ){
            if (kvp.Value != im.paramAr[kvp.Key]) {
                reParams = false;
                break;
            }
        }
        return reParams;
    }
}
class Order
{

    public String or_code { get; set; }
    public List <Item> items { get; set; }
    ...
    public override bool Equals( Object obj ){
        Order o = new Order();
        if (obj is Order)
            o = (Order)obj;
        else
            return false;

        if (this.or_code != String.Empty && o.or_code != String.Empty)
            if (this.or_code.Equals(o.or_code))
                return true;
        bool flag = true;
        foreach( Item i in  items){
            if (!o.items.Contains(i)) { 
                flag = false;
                break;
            }
        }
        return flag;
    }
}

EDIT:
i get this warning:

Warning : ‘Item’ overrides Object.Equals(object o) but does not
override Object.GetHashCode()

  • 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. Editorial Team
    Editorial Team
    2026-05-31T16:23:45+00:00Added an answer on May 31, 2026 at 4:23 pm

    Firstly, as I think you understand, wherever you implement Equals you MUST also implement GetHashCode. The implementation of GetHashCode must reflect the behaviour of the Equals implementation but it doesn’t usually use it.

    See http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx – especially the “Notes to Implementers”

    So if you take your example of the Item implementation of Equals, you’re considering both the values of id and name to affect equality. So both of these must contribute to the GetHashCode implementation.

    An example of how you could implement GetHashCode for Item would be along the lines of the following (note you may need to make it resilient to a nullable name field):

    public override GetHashCode()
    {
        return id.GetHashCode() ^ name.GetHashCode();
    }
    

    See Eric Lippert’s blog post on guidelines for GetHashCode – http://ericlippert.com/2011/02/28/guidelines-and-rules-for-gethashcode/

    As for whether you need to re-implement GetHashCode in subclasses – Yes if you also override Equals – as per the first (and main) point – the implementation of the two must be consistent – if two items are considered equal by Equals then they must return the same value from GetHashCode.

    Side note:
    As a performance improvement on your code (avoid multiple casts):

    if ( obj is Param){
        Param p = (Param)(obj);
    
    Param p = obj as Param;
    if (p != null) ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some classes in my app that don't require an ID to be
I have developed some classes with similar behavior, they all implement the same interface.
I have implemented drag and drop in my application, but am having some difficulty
I have a new error with the code below. Originally I had some classes
I have some classes layed out like this class A { public virtual void
I have some classes that will do something based on some conditions . The
I have some classes that represent immutable objects (Quantity, Price, Probability). Is there some
I have some classes which extends a superclass, and in the JSP I want
I have some classes that, for one reason or another, cannot be or need
Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {

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.