When performing OO design, is it better to consolidate a collection of methods that use simple value types into a generic method ? For example:
public int Sum(int x, int y)
// Overload with float.
public float Sum(float x, float y)
Consolidated to:
public T Sum<T> (T x, T y)
Thanks,
Scott
In general yes, however, specifically with your example (summing two arguments) – you can’t do it with generics, since the
+operator is not part of any interface to which you can constrain your generic type argument 🙂(Note: this is my C# part talking, in other languages that might still be possible)