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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:35:42+00:00 2026-05-11T01:35:42+00:00

Can anyone tell me how is ObjectIDGenerator better (worse?) then using HashSet when traversing

  • 0

Can anyone tell me how is ObjectIDGenerator better (worse?) then using HashSet when traversing an hierarchy of objects (that might be recurvise/circular), and not wanting to traverse the same object twice?

  • 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-11T01:35:43+00:00Added an answer on May 11, 2026 at 1:35 am

    The basic difference is in how each one does equality.

    ObjectIdGenerator looks at referential identity. When checking to see if an object is present already it will simply do an == call on the two object instances. This will boil down to a reference comparison because the objects are statically type to be object at this point. This is fine unless your object explicitly uses .Equals() for equality. If two objects are equal via .Equals() but different references, ObjectIDGenerator will consider them different objects. Likely not what you want.

    HashSet on the other hand allows you to customize the way in which you compare objects via the IEqualityComparer<T> parameter. If none is specified it will use EqualityComparer<T>.Default which will use value equality. This method will call into .Equals() and depend on it to determine if two objects are equal. In the case where you didn’t define a .Equals() method for your types it will default back to reference equality which is almost certainly what you want.

    In short, go with HashSet 🙂

    Sample Code showing the difference:

    class Person {     public readonly string Name;     public Person(string name) { Name = name; }     public override int GetHashCode()     {         return Name.GetHashCode();     }     public override bool Equals(object obj)     {         var other = obj as Person;         if (other == null)         {             return false;         }         return StringComparer.Ordinal.Equals(Name, other.Name);     } }  public static void Example() {     var gen = new ObjectIDGenerator();     bool isFirst;     var person1 = new Person('John');     var person2 = new Person('Bob');     gen.GetId(person1, out isFirst);    // isFirst = true     gen.GetId(person1, out isFirst);    // isFirst = false     gen.GetId(person2, out isFirst);    // isFirst = true     gen.GetId(new Person('John'), out isFirst); // isFirst = true even though they are .Equals()      var set = new HashSet<Person>();     set.Add(person1);     var contains1 = set.Contains(person1);              // true     var contains2 = set.Contains(new Person('John'));   // true } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer In my experience, the simplest way to check for results… May 11, 2026 at 1:30 pm
  • added an answer EDIT: As pointed out by Seb, this is not strictly… May 11, 2026 at 1:30 pm
  • added an answer It depends on the proxy they're going through. The Wikipedia… May 11, 2026 at 1:30 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.