如何在Visual C#中获得最多2个数字?

neu*_*cer 2 c# arrays methods function

我有一个包含五个数字的数组和一个包含2个数字的数组.我如何找出这7个数字中最大的数字?有没有一种方法可以让事情变得更容易?

Sam*_*ell 28

int[] array1 = { 0, 1, 5, 2, 8 };
int[] array2 = { 9, 4 };

int max = array1.Concat(array2).Max();
// max == 9
Run Code Online (Sandbox Code Playgroud)


Adr*_*der 10

你可以试试

decimal max = Math.Max(arr1.Max(), arr2.Max());
Run Code Online (Sandbox Code Playgroud)

  • +1不浪费时间和内存首先连接数组 (2认同)

RCI*_*CIX 5

直接的方法:

Math.Max(Math.Max(a,b), c)//on and on for the number of numbers you have
Run Code Online (Sandbox Code Playgroud)

使用 LINQ:

int[] arr1;
int[] arr2;
int highest = (from number in new List<int>(arr1).AddRange(arr2)
               orderby number descending
               select number).First();
Run Code Online (Sandbox Code Playgroud)