A bit of a conceptual question:
I'm creating my custom structure, in the vein of a Vector3 (3 int values) and I was working through overloading the standard operators (+,-,*, /, == etc...)
As I'm building a library for external use, I'm attempting to conform to FxCop rules. As such, they recommend having methods that perform the same function.
Eg. .Add(), .Subtract(), etc...
To save on code duplication, one of these methods (the operator overload, or the actual method) is going to call the other one.
My question is, which should call which?
Is it (and this is only an example code):
A)
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:
B)
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 either is preferable, but I'm looking for some opinions :)