In this blog post, I will be demonstrating how you can find the largest and smallest number in an array. Consider the following code snippet:
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
static void Main(string[] args) { List<int> list = new List<int>(); list.Add(3); list.Add(5); list.Add(12); list.Add(7); list.Add(14); Console.WriteLine("Biggest element in the list is: " + list.Max()); Console.WriteLine("Smallest element in the list is: " + list.Min()); Console.ReadLine(); } |
This code uses the Max and Min methods. The Collections class contains static utility methods that operate on collections like List, Set etc.
The Max() method returns the greatest element in the list according to the natural order or sorting.
Similarly, the Min() returns the smallest element in the list according to the natural order of sorting. When this code is executed, it will print the following output: