override GetHashCode help

I have my class properties like this:
//Properties
public int Id { get; set; }
public Transform Trs { get; set; }
public List<AS_Node> childList { get; set; }

And my overrides are like this:

	//Overrides
	public override bool Equals(object obj) {
		return (this.Id == ((AS_Node)obj).Id);
	}
	public override int GetHashCode() {
		return this.Id.GetHashCode();
	}
	public static bool operator ==(AS_Node one, AS_Node two) {
		return one.Equals(two);
	}
	public static bool operator !=(AS_Node one, AS_Node two) {
		return !one.Equals(two);
	}

My question is, is this the right way of doing it?

I believe your Equals is missing. What if you passed it a null, or any other class?
Add to your Equals:

public override bool Equals(object obj) 
{       
        AS_Node other = obj as AS_Node;
        if (other != null)
            return (this.Id == other.Id);
}

Your code contains error, too. What if other is null?

public override bool Equals(object obj) 
{      
        AS_Node other = obj as AS_Node;

        if (other != null)
            return (this.Id == other.Id);
        else return false;

        // Or even shorter:
        // return other != null  this.Id == other.Id;
}

I’m having trouble overriding GetHashCode, did you have any problems? - Can you help me out? (or stack overflow)

@Maklaud: Sorry, I forgot to add return false at the end, missed it. Your else statement is redundant btw.

It’s just the matter of style :slight_smile: On my work we had different coding rules, and used different styles. Of course, it’s possible to return false without an else statement.