SoFunction
Updated on 2025-03-06

List collection in C# uses Max method to find the instance of the maximum value

In the List collection operation of C#, sometimes it is necessary to find the maximum value in the List collection. At this time, you can use the extension method of the List collection, Max method. There are two forms of the Max method. One is a form without any parameters, which is suitable for the List collection of some value type variables, and the other is written with Lambda expressions. This method can be used to obtain the maximum value of a certain attribute in the List collection.

(1) Take an example of the Max method form without any parameters, the program call form is as follows:

List list1 = new List() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var maxValue = ();

The calculation result is: maxValue=10.

(2) Examples of Max methods with Lambda expression writing form

We need to get the maximum value of the object property Index in the List collection object testList collection. First, let’s look at the definition of TestModel:

public class TestModel
{
public int Index { set; get; }

 public string Name { set; get; }
}

To get the maximum Index attribute value of all objects in the testList collection, use the following statement:

List testList = new List<>();

var max = (t => );

Supplementary knowledge:List collection in C# uses LastOrDefault method to find out the last element that meets the criteria

In the List collection of C#, the FirstOrDefault method is generally used to find the first object in the List collection that meets the conditions. If it is not found, it returns the corresponding default value. In fact, if you want to find the last List collection element object that meets the conditions, you can use the LastOrDefault method. The internal writing form of the LastOrDefault method is the writing form of the Lambda representation.

If the LastOrDefault method does not find an element object that meets the requirements, it will return the corresponding default value.

(1) Suppose there is a List collection object list1, and the internal elements are 1 to 10. In actual operations, we do not know the specific elements in the list1 set, but we need to find the last element object that meets the conditions greater than 7 in the list1 set. You can use the following statement.

List list1 = new List() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var value=(t => t > 7);

(2) If the List collection stores a reference type object, such as a custom class, we can also find the last element object that meets the criteria based on a certain attribute.

For example, there is a List collection object testList, and the definition of the TestModel class is as follows:

public class TestModel
{
public int Index { set; get; }

  public string Name { set; get; }
}

We need to find out the last element object that meets the criteria with the Index value in the testList with an Index value greater than 7. You can use the following statement:

var model=(t=>>7);

In the above two examples, t=>t is the statement writing of the Lambda expression, and t represents the element object in the List collection.

The example of using the Max() method to find the maximum value in the List collection in C# is all the content I share with you. I hope you can give you a reference and I hope you can support me more.