回答:
さて、あなたはそれを2回呼び出すことができます:
int max3 = Math.Max(x, Math.Max(y, z));
これを頻繁に行う場合は、いつでも独自のヘルパーメソッドを作成できます...これを私のコードベースで一度見れば十分ですが、定期的ではありません。
(これは、AndrewのLINQベースの回答よりも効率的である可能性が高いことに注意してください。ただし、明らかに要素数が多いほど、LINQアプローチは魅力的です。)
編集:「両方の長所」のアプローチは、いずれかの方法でメソッドのカスタムセットを持つことです。
public static class MoreMath
{
// This method only exists for consistency, so you can *always* call
// MoreMath.Max instead of alternating between MoreMath.Max and Math.Max
// depending on your argument count.
public static int Max(int x, int y)
{
return Math.Max(x, y);
}
public static int Max(int x, int y, int z)
{
// Or inline it as x < y ? (y < z ? z : y) : (x < z ? z : x);
// Time it before micro-optimizing though!
return Math.Max(x, Math.Max(y, z));
}
public static int Max(int w, int x, int y, int z)
{
return Math.Max(w, Math.Max(x, Math.Max(y, z)));
}
public static int Max(params int[] values)
{
return Enumerable.Max(values);
}
}
そうすれば、あなたは書くことができますMoreMath.Max(1, 2, 3)またはMoreMath.Max(1, 2, 3, 4)配列作成のオーバーヘッドなしに、まだ書きMoreMath.Max(1, 2, 3, 4, 5, 6)ますが、オーバーヘッドを気にしないときの素敵な読みやすく、一貫性のあるコードのために。
個人的には、LINQアプローチの明示的な配列作成よりも読みやすいと思います。
MoreMath.Max(x, y, z)は、LINQのアプローチであるIMOよりも読みやすくなっています。
public static int Max(params int[] values) ですか?
Max(1, 2, 3)理由もなく配列が作成されます。比較的少数のパラメーターにいくつかのオーバーロードを提供することにより、呼び出し元の読みやすさに影響を与えることなく、より効率的にすることができます。
あなたが使うことができますEnumerable.Max:
new [] { 1, 2, 3 }.Max();
[]。気の利いた。
new int[] { 1,2,3 }。したがって、これはint型の配列であり、その内容によって暗黙的に決定されます。
LinqにはMax関数があります。
あなたが持っているIEnumerable<int>場合はこれを直接呼び出すことができますが、これらを別々のパラメータで必要とする場合は次のような関数を作成できます:
using System.Linq;
...
static int Max(params int[] numbers)
{
return numbers.Max();
}
次に、次のように呼び出すことができます:max(1, 6, 2)、任意の数のパラメーターを許可します。
Maxではなく、それを呼び出しmaxて静的にしたいのを除いて、静的にします:)少数のパラメーターでオーバーロードすることで、効率を上げることもできます。
ジェネリックとして
public static T Min<T>(params T[] values) {
return values.Min();
}
public static T Max<T>(params T[] values) {
return values.Max();
}
トピック外ですが、ここにミドルバリューの公式があります。誰かがそれを探している場合に備えて
Math.Min(Math.Min(Math.Max(x,y), Math.Max(y,z)), Math.Max(x,z));
何らかの理由(スペースエンジニアAPIなど)で、System.arrayにMaxの定義がない場合、またはEnumerableにアクセスできない場合、n個の値のMaxの解決策は次のとおりです。
public int Max(int[] values) {
if(values.Length < 1) {
return 0;
}
if(values.Length < 2) {
return values[0];
}
if(values.Length < 3) {
return Math.Max(values[0], values[1]);
}
int runningMax = values[0];
for(int i=1; i<values.Length - 1; i++) {
runningMax = Math.Max(runningMax, values[i]);
}
return runningMax;
}
この関数は整数の配列を取ります。(配列の送信に関する@Jon Skeetの不満を完全に理解しています。)
それはおそらく少しやり過ぎです。
public static int GetMax(int[] array) // must be a array of ints
{
int current_greatest_value = array[0]; // initializes it
for (int i = 1; i <= array.Length; i++)
{
// compare current number against next number
if (i+1 <= array.Length-1) // prevent "index outside bounds of array" error below with array[i+1]
{
// array[i+1] exists
if (array[i] < array[i+1] || array[i] <= current_greatest_value)
{
// current val is less than next, and less than the current greatest val, so go to next iteration
continue;
}
} else
{
// array[i+1] doesn't exist, we are at the last element
if (array[i] > current_greatest_value)
{
// current iteration val is greater than current_greatest_value
current_greatest_value = array[i];
}
break; // next for loop i index will be invalid
}
// if it gets here, current val is greater than next, so for now assign that value to greatest_value
current_greatest_value = array[i];
}
return current_greatest_value;
}
次に、関数を呼び出します。
int highest_val = GetMax (new[] { 1,6,2,72727275,2323});
// highest_val = 72727275