- Depends on you, by default == and != will redirect to the default Equals implementation but it's considered better practice to use Equals directly instead of == or !=
- You should, by default Equals performs bitwise comparison on value types and reference equality on reference types, so if your struct contains fields that are reference types, you will need to override Equals to perform a proper comparison on them. Besides that, MSDN recommends to override Equals anyway, for performance benefits as well.
Particularly if your value type contains fields that are reference
types, you should override the Equals(Object) method. This can improve
performance and enable you to more closely represent the meaning of
equality for the type.
- Structs are copied by default when passed between variables/methods, so there is no need to write a copy constructor. The only case I can think of is if your structs contains fields that are reference types and you want to create a copy constructor that performs deep copy on them, up to you.
- What do you mean worry about assignment?
- Yes. If you're going to use your struct as a member of HashSet or as a key in a Dictionary, you would want to provide a custom implementation of GetHashCode.
It is best to implement IEquatable to avoid boxing (default Equals accepts an object type, so your struct will have to undergo boxing to fit that type).
Collections (Arrays, Dictionaries, etc...) will usually check if their members implement IEquatable, and will use IEquatable.Equals, so it's best to implement it.
It is also recommended to implement the default Equals (from System.Object) and direct to to the IEquatable.Equals implementation.
Example:
struct MyStruct : IEquatable<MyStruct>
{
public int A { get; set; }
public int B { get; set; }
public bool Equals(MyStruct other)
{
return A == other.A && B == other.B;
}
public override bool Equals(object obj)
{
if (!(obj is MyStruct)) return false;
return ((MyStruct)obj).Equals(this);
}
public override int GetHashCode()
{
return (A, B).GetHashCode();
}
}
I also provided an example of how to implement GetHashCode, best way is to use the implementation of Tuple.
Again, implementing == and != is up to you
The standard library (System, System.Collections, etc...) uses System.Equals or IEquatable.Equals to do comparison, and you should too.
So no, implementing == and != is not necessary.