-3

I'm making use of HashSet's for algorithmic purposes, but I am having problems with the implementation of "custom objects". Doing some research it appears one should:

  • Override Equals and GetHashCode (previous question here)
  • Should not generate hashcodes with mutable objects (here yes it's Java, but I figured the implementation was close enough)

My Implementation:

//Simplified version of actual class with key components
class customObject {
    public readonly uint column;
    public readonly char row;

    public customObject(uint column, char row) {
        this.column = column;
        this.row = row;
    }

    public override bool Equals(object obj) {
        return obj is customObj && !ReferenceEquals(this, obj);
    }

    /*where uint column and char row are readonly. HashSet add is not called
    until these variables are set with their lifetime value.*/
    public override int GetHashCode() {
        unchecked {
            var hashCode = row.GetHashCode();
            hashCode = (hashCode * 397) ^ column.GetHashCode();
            return hashCode;
        }
    }
}

//somwhere else
HashSet<customObject> s = new HashSet<customObject>();
for(int i = 0; i < 10; i++) {
    for(char c = 'A'; c < 'J'; c++) {
        s.add(new customObject((uint)i,c));
    }
 }

Unfortunately, I am unable to add my custom object to the HashSet. As I can confirm the count of items in the HashSet is 0 after attempting to add the object. Since there are no items in the set I suspect that I am missing something in the implementation, but have been unable to find a complete answer/tutorial regarding prepping custom objects for use in HashSets.

Community
  • 1
  • 1
Bennett Yeo
  • 819
  • 2
  • 14
  • 28
  • first your `Equals` is wrong . Second it's hard to see the problem with your Hashset when you don't post the code for this as well (and btw: add the definition of your object here too ... for example: *what* is `row`?) – Random Dev Jun 22 '15 at 04:49
  • 1
    The code looks OK (except I'm not sure why your `ReferenceEquals` is negated). How are you adding the object to your `HashSet`? Are you getting any exceptions? – vesan Jun 22 '15 at 04:49
  • @vesan I'm adding with set.add(obj) – Bennett Yeo Jun 22 '15 at 04:50
  • 1
    please post **real** *working* code... – Random Dev Jun 22 '15 at 04:51
  • Please post a [MCVE](http://stackoverflow.com/help/mcve). The reason why your `HashSet` is empty is almost certainly unrelated to the code you have posted, unless your `GetHashCode` method throws an exception. – vesan Jun 22 '15 at 04:58
  • @vesan I was looking more to check my steps as far as went for the customObject, so I originally didn't think a working example would be necessary. I have revised "my implementation" section to more reflect my situation. – Bennett Yeo Jun 22 '15 at 05:01
  • 1. Your code does not compile. 2. `for(char c = 'A'; c < 'A'; c++)` will never enter the loop, as C will never be < 'A'. – vesan Jun 22 '15 at 05:01
  • @vesan that was a typo, that should be some other greater value. That wasn't causing the issue unfortunately. – Bennett Yeo Jun 22 '15 at 05:04
  • 1
    1. Your code still does not compile. 2. If I fix all syntax errors, it works as expected. Post the _actual_ code you are running that exhibits the problem. – vesan Jun 22 '15 at 05:07

1 Answers1

1
for(char c = 'A'; c < 'A'; c++)

The debugger is your friend; check your loop condition. That loop body will never run. Also, your implementation of GetHashCode would never cause Count == 0 after adding your object with no exception being raised. Lastly, your implementation of Equals is wrong.

It assures that the same object reference will not be considered equal and, if you're going for reference equality, then you don't need to override it in the first place.

Ed S.
  • 122,712
  • 22
  • 185
  • 265