A bit of a conceptual question:
I create my custom structure in the spirit of Vector3 (3 int values), and I worked with overloading standard operators (+, -, *, /, ==, etc.)
Since I am creating a library for external use, I am trying to comply with FxCop rules. Therefore, they recommend using methods that perform the same function.
Eg..Add (),. Subtract (), etc.
To save code duplication, one of these methods (operator overloading or the actual method) will call the other.
My question is, which should be called, which?
This (and this is just code example):
BUT)
public static MyStruct operator +(MyStruct struc1, MyStruct struct2) { return struc1.Add(struct2); } public MyStruct Add(MyStruct other) { return new MyStruct ( this.X + other.X, this.Y + other.Y, this.Z + other.Z); }
or
IN)
public static MyStruct operator +(MyStruct struc1, MyStruct struct2) { return new MyStruct ( struct1.X + struct2.X, struct1.Y + struct2.Y, struct1.Z + struct2.Z); } public MyStruct Add(MyStruct other) { return this + other; }
I'm really not sure if this is preferable, but I'm looking for a few opinions :)
Alastair pitts
source share