I need to find the minimum between 3 values, and I ended up doing something like this:
Math.Min(Math.Min(val1, val2), val3)
It just seems a little silly to me, because other languages use variadic functions for this. I highly doubt this was an oversight though.
Is there any reason why a simple Min/Max function shoundn’t be variadic? Are there performance implications? Is there a variadic version that I didn’t notice?
If it is a collection (A subclass of
IEnumerable<T>) one could easily use the functions in theSystem.LinqlibraryFurthermore, it’s easy to implement these methods on your own:
You can call these methods with just simple scalars so
Maths.Min(14,25,13,2)would give2.These are the generic methods, so there is no need to implement this method for each type of numbers (
int,float,…)I think the basic reason why these methods are not implemented in general is that, every time you call this method, an array (or at least an
IListobject) should be created. By keeping low-level methods one can avoid the overhead. However, I agree one could add these methods to theMathclass to make the life of some programmers easier.