-6

I am working on a FastReport that uses C#. I am trying to get the maximum value of 10 variables (heart rates).

I have the following code written:

public static int GetMaxHeartRate(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j)
{
  int result = 0;

  result = Math.Max(a, b, c, d, e, f, g, h, i, j);
  
  return result;
}

I am getting this error: "Error CS1501: No overload for method 'Max' takes 10 arguments". I know how to use the Math.Max for 3 variables, but can't figure it out for 10 variables.

elderboy02
  • 27
  • 5
  • You'll need to do a Max of each of the Max of the pairs: `Math.Max(a, Math.Max(b, ...` – Camilo Terevinto Dec 29 '20 at 17:04
  • 1
    Why not simply add them to a list and sort that list? – QBrute Dec 29 '20 at 17:06
  • 4
    Well, Max admits 2 arguments and returns the maximum value between those 2. You can solve your problem this way `var max = (new List() { a, b, c, d, e, f, g, h, i, j}).Max();` – Dorin Baba Dec 29 '20 at 17:08
  • Another duplicate: [Find max value of two (or more) properties in list](https://stackoverflow.com/questions/13816094/find-max-value-of-two-or-more-properties-in-list) –  Dec 29 '20 at 17:09
  • 1
    @OlivierRogier I don't think so. It's relatively easy to get the maximum number from only 3 value (You have to use Max function twice). My boi wants the max value from 10 elements, writing 1km of (Max(Max(Max(Max())))) is not nice. – Dorin Baba Dec 29 '20 at 17:11
  • 1
    @DorinBaba use an array for that, not a list – Caius Jard Dec 29 '20 at 17:14
  • @DorinBaba The duplicates contains many methods like using recursive Max or array/list as well as Linq. –  Dec 29 '20 at 17:14

2 Answers2

4

Your life might be a bit easier if you use params in this context:

public static int GetMaxHeartRate(params int[] v)
{
  return v.Max();
}

You can call it like GetMaxHeartRate(1,2,3,4,5,6,7,8,9), GetMaxHeartRate(myInt1, myInt2, myInt3, myInt4) or even a crazy mix of stuff like GetMaxHeartRate(a, 20, DateTime.Now.Hour, int.Parse("300")) - it'll find the max of any number of int inputs; the compiler will take all the individual ints you've supplied (however they look) and pack them into an array for you.

The only thing you need to make it work beyond this, is to ensure that you have using System.Linq; at the top of your class, because the Max() is a LINQ extension method that works on an array, list etc

You can also call it with an array if you're getting an array from somewhere else, like if you've loaded these from a file..

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • Is there any special reason for params that I don't see here? By using the params keyword, you can specify variability on number of arguments. – S.N Dec 29 '20 at 17:30
  • Did you just answer your own question? `params` essentially enlists the help of the compiler in converting the OP's `a,b,c,d,e,f,g,h,i,j` to an array so that LINQ's Max can be called - I looked at the method signature and figured that they clearly have, somewhere else, a call like `GetMaxHeartRate(1,2,3,4,5,6,7,8,9,10)` - using params here means they don't have to modify that there, and this code gets nicer, shorter, easier to read, and bonus - no longer locked to finding the Max of 10 inputs – Caius Jard Dec 29 '20 at 17:32
  • I assume, the variable number of elements will be handled by int[]. But I am not sure why params, as there is no need for variability in number of parameters. – S.N Dec 29 '20 at 17:34
  • You cannot have (elsewhere) a call like `GetMaxHeartRate(1,2,3,4,5,6,7,8,9)` for a method defined as `GetMaxHeartRate(int[] x)` - without the `params`, you have to form the array. With params, the compiler forms the array – Caius Jard Dec 29 '20 at 17:36
0

Use Enumerable.Max Method and here enumerable is your collection of integer values.

class Program
{
    static void Main(string[] args)
    {
        var input = new[] {1, 12, 99, 23, 12, 45, 23, 33, 44, 22, 89, 23, 223, 32, 355};
        var result = GetMaxHeartRate(input);
    }
    public static int GetMaxHeartRate(int[] values)
    {
        return values?.Max() ?? 0;
    }
}
S.N
  • 4,910
  • 5
  • 31
  • 51