How to get the largest element of an array in C#
In this tutorial, we are going to learn about how to find the largest element of an array in C#.
Consider, that we have the following array.
int[] arr = { 20, 40, 60, 80, 70};
Using Max() method
To get the largest element of an array, we can use the Max()
method in C# which is available inside System.Linq
namespace.
Here is an example :
using System;
using System.Linq;
class GetLargestElement {
static void Main() {
int[] arr = { 20, 40, 60, 80, 70};
int largeElement = arr.Max();
Console.WriteLine(largeElement);
}
}
Output:
80