TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Max and Min Methods

This C# article demonstrates the Max and Min methods from System.Linq.

Max, Min. A collection has a maximum and minimum value.

The Max and Min methods can be used to find these. Max looks through the source collection and returns the largest value. Min inversely finds the smallest value.

Tip: These can also be used with lambda expressions. The result of this lambda expression is used.

Lambda

Max. To begin, this program instantiates an array of four integers on the heap. It contains the values -2 through 1. Next the Max method is called upon this array. Finally, we use another version of Max that receives a lambda expression.

And: This translates all the values into their absolute values before comparing them.

C# program that uses Max method

using System;
using System.Linq;

class Program
{
    static void Main()
    {
	int[] array1 = { 1, -1, -2, 0 };

	// Find maximum number.
	Console.WriteLine(array1.Max());

	// Find maximum number when all numbers are made positive.
	Console.WriteLine(array1.Max(element => Math.Abs(element)));
    }
}

Output

1
2

The Max method returns 1 when called with no parameters. This is because 1 is the largest value. When the Math.Abs transformation is used, though, the value 2 is returned because the absolute value of -2 is 2, and this is larger than 1.

Note: The original value is not returned. Instead the value transformed by the lambda expression is.

Math.Abs

Min. Next we want to find the minimum value in a collection (such as an array or List) in a simple way. With the Min method we find the minimum element or the minimum value after a transformation.

ArraysList

This program instantiates an array of integers upon execution. Then, the Min() method is called on the array. Finally, the Min() method is called again, but with a lambda expression that makes all values negative.

C# program that uses Min method

using System;
using System.Linq;

class Program
{
    static void Main()
    {
	int[] array1 = { 1, -1, 2, 0 };

	// Find minimum number.
	Console.WriteLine(array1.Min());

	// Find minimum number when all numbers are made negative.
	Console.WriteLine(array1.Min(element => -element));
    }
}

Output

-1
-2

The first call to Min determines that -1 is the smallest integer. The second call changes the 2 to -2, so that is now the smallest integer. Note that the values in the source array (array1) are not mutated.

Tip: The Min method provides a clear way to find the smallest value in a collection.

Also: You can provide a transformation function, which provides a mechanism for you to insert additional logic if needed.

Summary. The Max function can find either the maximum value in a collection, or the maximum value after a specific transformation is applied, such as an absolute value. Max is precisely opposite the Min function—Min returns the minimum value.

Also: These methods are perhaps most useful with a lambda expression: this makes the computation adjustable.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf