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?

  • 2
    not what your looking for, but LINQ has a min extension for IEnumerable – Muad'Dib Mar 15 '12 at 3:02
  • 7
    Calling a variadic function allocates an array. This may have been considered too much overhead for a simple min/max. – arx Mar 15 '12 at 3:07
up vote 31 down vote accepted

If it is a collection (A subclass of IEnumerable<T>) one could easily use the functions in the System.Linq library

int min = new int[] {2,3,4,8}.Min();

furthermore it's easy to implement these methods on your own:

public static class Maths {

    public static T Min<T> (params T[] vals) {
        return vals.Min();
    }
    public static T Max<T> (params T[] vals) {
        return vals.Max();
    }

}

You can call this methods with just simple scalars so Maths.Min(14,25,13,2) would give 2.

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 IList object should be created. By keeping low level methods one can avoid the overhead. However I agree one could add these methods to the Math class to make the life of some programmers more easy.

  • 2
    I think now it does, furthermore most people want a pragmatic solution. There are other stackexchange sites for theoretical computer science and programming language paradigms. – Willem Van Onsem Mar 15 '12 at 3:11
  • 1
    @CommuSoft: If he has asked how to do it, then your original answer would have worked, but he asked why. While where the question should be asked is debatable, typically abstract questions about specific APIs are allowed, since obviously this question isn't a general one about computer science (although one could change it to be one...) – Guvante Mar 15 '12 at 3:15
  • 2
    I said the answer is probably that using an array or collection slows down the performance of the method. If I am correct the methods don't even exists. They are an alias for the low level code (for instance the MINPS in 80x86 architecture). I also included a way to solve this problem without having to write 2 methods for every type of numeric representation because most people who will find this topic are only looking for a solution. – Willem Van Onsem Mar 15 '12 at 3:22
  • 2
    I guess this is a decent answer. I was more looking into why C# didn't implement it, but I guess this is solved using Linq. Thanks for the info! – beatgammit Mar 15 '12 at 3:35
  • 7
    @DJQuimby: Rather than complaining about this answer, why not write your own that you like better? – Eric Lippert Mar 15 '12 at 4:25

CommuSoft has addressed how to accomplish the equivalent in C#, so I won't retread that part.

To specifically address your question "Why aren't C#'s Math.Min/Max variadic?", two thoughts come to mind.

First, Math.Min (and Math.Max) is not, in fact, a C# language feature, it is a .NET framework library feature. That may seem pedantic, but it is an important distinction. C# does not, in fact, provide any special purpose language feature for determining the minimum or maximum value between two (or more) potential values.

Secondly, as Eric Lippert has pointed out a number of times, language features ( and presumably framework features) are not "removed" or actively excluded - all features are unimplemented until someone designs, implements, tests, documents and ships the feature. See here for an example.

Not being a .NET framework developer, I cannot speak to the actual decision process that occurred, but it seems like this is a classic case of a feature that simply never rose to the level of inclusion, similar to the sequence foreach "feature" Eric discusses in the provided link.

  • 1
    Yeah, that was a little pedantic, but that's what I expect from SO. I know the difference between a language built-in and a library feature (I mean, you it's namespaced to Math). I should have said .NET and Mono (the two major implementations, which have nearly identical standard libs). Decent link, BTW though. – beatgammit Mar 15 '12 at 3:39
  • @tjameson in that case, you might want to change the title and the tag on the question since, as you state, this is not in fact a C# question. Thanks. – JeremyDWill Mar 15 '12 at 3:44
  • If it's really bugging you, go ahead and edit it. I can't think of a way to include .NET and Mono without making the title super long. I think it's fine how it is. I'm not aware of a C# implementation that doesn't have an equivalent function in the standard library. – beatgammit Mar 15 '12 at 20:10

I think CommuSoft is providing a robust answer that is at least suited for people searching for something along these lines, and that should be accepted.

With that said, the reason is definitely to avoid the overhead necessary for the less likely use case that people want to compare a group rather than two values.

As pointed about by @arx, using a parametric would be unnecessary overhead for the most used case, but it would also be a lot of unnecessary overhead with regards to the loop that would have to be used internally to go through the array n - 1 times.

I can easily see an argument for having created the method in addition to the basic form, but with LINQ that's just no longer necessary.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.