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

  • SEARCH
  • Home
  • 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 6685449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:03:32+00:00 2026-05-26T05:03:32+00:00

It seems that this problem has already been encountered by quite a few people:

  • 0

It seems that this problem has already been encountered by quite a few people:

List not working as expected

Contains always giving false

So I saw the answers and tried to implement the override of Equals and of GetHashCode but there seems that I am coding something wrong.

This is the situation: I have a list of Users(Class), each user has a List and a Name property, the list property contains licenses. I am trying to do a

         if (!users.Contains(currentUser))

but it is not working as expected. And this is the code I did to override the Equals and GetHashCode:

    public override bool Equals(object obj)
    {
        return Equals(obj as User);
    }


    public bool Equals(User otherUser)
    {
        if (ReferenceEquals(otherUser, null))
            return false;

        if (ReferenceEquals(this, otherUser))
            return true;

        return this._userName.Equals(otherUser.UserName) && 
               this._licenses.SequenceEqual<string>(otherUser.Licenses);
    }

    public override int GetHashCode()
    {
        int hash = 13;
        if (!_licenses.Any() && !_userName.Equals(""))
        {
            unchecked
            {
                foreach (string str in Licenses)
                {
                    hash *= 7;
                    if (str != null) hash = hash + str.GetHashCode();
                }
                hash = (hash * 7) + _userName.GetHashCode();
            }
        }
        return hash;
    }

thank you for your suggestions and help in advance!

EDIT 1:

this is the code where I am doing the List.Contains, I am trying to see if the list already contains certain user, if not then add the user that isn’t there. The Contains only works the first time, when currentUser changes then the User inside the list changes to the current user maybe this is a problem that is unrelated to the equals, any ideas?

        if (isIn)
        {
            if (!listOfLicenses.Contains(items[3]))
                listOfLicenses.Add(items[3]);

            if (!users.Contains(currentUser))
            {
                User user2Add = new User();
                user2Add.UserName = currentUser.UserName;
                users.Add(user2Add);
                userIndexer++;
            }

            if (users[userIndexer - 1].UserName.Equals(currentUser.UserName))
            {
                users[userIndexer - 1].Licenses.Add(items[3]);
            }
            result.Rows.Add();
        }
  • 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-26T05:03:32+00:00Added an answer on May 26, 2026 at 5:03 am

    Well, one problem with your hash code – if either there are no licences or the username is empty, you’re ignoring the other component. I’d rewrite it as:

    public override int GetHashCode()
    {
        unchecked
        {
            int hash = 17;
            hash = hash * 31 + _userName.GetHashCode();
            foreach (string licence in Licences)
            {
                hash = hash * 31 + licences.GetHashCode();
            }
            return hash;
        }
    }
    

    Shorter and simpler. It doesn’t matter if you use the hash code of the empty string, or if you iterate over an empty collection.

    That said, I’d have expected the previous code to work anyway. Note that it’s order sensitive for the licences… oh, and List<T> won’t use GetHashCode anyway. (You should absolutely override it appropriately, but it won’t be the cause of the error.)

    It would really help if you could show a short but complete program demonstrating the problem – I strongly suspect that you’ll find it’s actually a problem with your test data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty sure that this question already has been answered somewhere. It just seems
It seems this is a known problem and has been asked several times before
I know that this question has already been asked in this post ( xts
Seems that This will be an easy question for you but this problem is
I'm really suprised I haven't run into this problem before, but it seems that
I have an issue that seems like very flaky behavour, is this a problem
This seems like a simple problem: I have a WF4 activity that guides the
My problem seems to be this: heap data allocated by one thread (that later
I've been searching around for the answer and everything seems to lead that this
I understand that double posts has been a problem with forms forever. I am

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.