Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't C#'s Math.Min/Max variadic?

Tags:

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?

like image 446
beatgammit Avatar asked Mar 15 '12 02:03

beatgammit


People also ask

Why is C not A or B?

Because C comes after B The reason why the language was named “C” by its creator was that it came after B language. Back then, Bell Labs already had a programming language called “B” at their disposal.

Is C very difficult?

It is not hard to learn C. Just like any other skill, you will need patience and resilience to master coding using C. The programming language features 32 keywords for its syntax. This makes it a relatively simple coding language to learn.

Did C+ exist?

C+ does exist. My mother used it in university in preC++ days. C# or C sharp is a programming language that is an improvement of the original C language C is a language commonly used for creating basic OS C+, or Holy C, is a language created by the schizophrenic Terry A.

Why is C not secured?

The top vulnerabilities found in C were buffer errors and input validation, the report reads, and although numbers have both risen and fallen since 2009, it remains the most insecure language. In C's defense, it should be noted that this is the oldest (and most widely used) programming language in the list.


2 Answers

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 these 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 easier.

like image 104
Willem Van Onsem Avatar answered Oct 09 '22 12:10

Willem Van Onsem


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.

like image 21
JeremyDWill Avatar answered Oct 09 '22 11:10

JeremyDWill