Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

So, I've got you bog standard c# object, overriding Equalsand implementing IEquatable

    public override int GetHashCode()
    {
        return _name.GetHashCode();
    }

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

    #region IEquatable<Tag> Members

    public bool Equals(Tag other)
    {
        if (other == null)
            return false;
        else
            return _name == other._name;
    }

    #endregion

Now, for some reason, I used to think that forwarding the calls from Equals into Equals was bad, no idea why, perhaps I read it a long time ago, anyway I'd write separate (but logically same) code for each method.

Now I think forwarding Equals to Equals is okay, for obvious reasons, but for the life me I can't remember why I thought it wasn't before.

Any thoughts?

share|improve this question
7  
Should this be on StackOverflow? –  FrustratedWithFormsDesigner Jan 31 '11 at 22:12
    
No idea. I can't think of a situation where you'd want to duplicate your comparison logic. That's just asking for trouble. Choosing whether to have Equals(object) calling Equals(Tag) or the other way around seems like a stylistic choice to me. –  Anna Lear Jan 31 '11 at 22:15
1  
@Anna: If Equals(Tag) calls Equals(object), then you have the runtime type-check regardless of which method is called. If Equals(object) checks the type and calls Equals(Tag), then you skip the check when the type is known. –  Anon. Jan 31 '11 at 22:20
    
@Anon.: Am I missing something? Sounds like there's a type check one way or another, regardless of who's doing it. Although I suppose you'd also incur a boxing cost if you go the Tag->object route, made worse by the fact that the Equals(object) implementation will probably unbox it, which is wasted effort if we know the type in the first place. So perhaps Equals(object)->Equals(Tag) is the better direction. –  Anna Lear Jan 31 '11 at 22:23
    
@Anna @ Anon Cheers Guys @FrustratedWithFormsDesigner Yea, I wasn't sure, I mean, as it focused more on practise than implemention, I thought it might be okay here. –  Jaimal Chohan Jan 31 '11 at 22:24

1 Answer 1

up vote 2 down vote accepted

This is good practice but you have to be careful if Tag was a struct. If that is the case you want to do this.

public override bool Equals(object obj)
{
    return obj is Tag && Equals((Tag)obj);
}
share|improve this answer
    
I wonder if there is a more efficient form ... –  Job Feb 1 '11 at 3:53
    
Wouldn't obj as Tag fail to compile if Tag was a struct? –  dan04 Jun 25 '11 at 21:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.