Skip to main content
.NET Framework Class Library
EnumerableMaxTSource, TResult Method (IEnumerableTSource, FuncTSource, TResult)

Invokes a transform function on each element of a generic sequence and returns the maximum resulting value.

Namespace:   System.Linq
Assemblies:   System.Linq (in System.Linq.dll)
  System.Core (in System.Core.dll)
<[%$TOPIC/bb534962_en-us_VS_110_1_0_0_0_0%]> _
Public Shared Function Max(Of TSource, TResult) ( _
	source As [%$TOPIC/bb534962_en-us_VS_110_1_0_0_0_1%](Of TSource), _
	selector As [%$TOPIC/bb534962_en-us_VS_110_1_0_0_0_2%](Of TSource, TResult) _
) As TResult
public static TResult Max<TSource, TResult>(
	this [%$TOPIC/bb534962_en-us_VS_110_1_0_1_0_0%]<TSource> source,
	[%$TOPIC/bb534962_en-us_VS_110_1_0_1_0_1%]<TSource, TResult> selector
generic<typename TSource, typename TResult>
static TResult Max(
	[%$TOPIC/bb534962_en-us_VS_110_1_0_2_0_1%]<TSource>^ source, 
	[%$TOPIC/bb534962_en-us_VS_110_1_0_2_0_2%]<TSource, TResult>^ selector
static member Max : 
        source:[%$TOPIC/bb534962_en-us_VS_110_1_0_3_0_0%]<'TSource> * 
        selector:[%$TOPIC/bb534962_en-us_VS_110_1_0_3_0_1%]<'TSource, 'TResult> -> 'TResult 
JScript does not support generic types or methods.

Type Parameters


The type of the elements of source.


The type of the value returned by selector.


Type: System.Collections.GenericIEnumerableTSource

A sequence of values to determine the maximum value of.

Type: SystemFuncTSource, TResult

A transform function to apply to each element.

Return Value

Type: TResult
The maximum value in the sequence.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableTSource. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

source or selector is .


If type TResult implements IComparableT, this method uses that implementation to compare values. Otherwise, if type TResult implements IComparable, that implementation is used to compare values.

In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.


The following code example demonstrates how to use MaxTSource(IEnumerableTSource, FuncTSource, Int32) to determine the maximum value in a sequence of projected values.


This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, change the body of the selector function.

Structure Pet
        Public Name As String 
        Public Age As Integer 
    End Structure 

    Sub MaxEx4()
        ' Create an array of Pet objects. 
        Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8}, _
                             New Pet With {.Name = "Boots", .Age = 4}, _
                             New Pet With {.Name = "Whiskers", .Age = 1}}

        ' Determine the "maximum" pet by passing a 
        ' lambda expression to Max() that sums the pet's age 
        ' and name length. 
        Dim max As Integer = pets.Max(Function(pet) _
                                          pet.Age + pet.Name.Length)

        ' Display the result.
        MsgBox("The maximum pet age plus name length is " & max)
    End Sub 

    ' This code produces the following output: 
    ' The maximum pet age plus name length is 14
class Pet
                public string Name { get; set; }
                public int Age { get; set; }

            public static void MaxEx4()
                Pet[] pets = { new Pet { Name="Barley", Age=8 },
                               new Pet { Name="Boots", Age=4 },
                               new Pet { Name="Whiskers", Age=1 } };

                int max = pets.Max(pet => pet.Age + pet.Name.Length);

                    "The maximum pet age plus name length is {0}.",

             This code produces the following output:

             The maximum pet age plus name length is 14.
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library