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

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

Namespace:   System.Linq
Assemblies:   System.Linq (in System.Linq.dll)
  System.Core (in System.Core.dll)
<[%$TOPIC/bb548659_en-us_VS_110_2_0_0_0_0%]> _
Public Shared Function Max(Of TSource) ( _
	source As [%$TOPIC/bb548659_en-us_VS_110_2_0_0_0_1%](Of TSource), _
	selector As [%$TOPIC/bb548659_en-us_VS_110_2_0_0_0_2%](Of TSource, [%$TOPIC/bb548659_en-us_VS_110_2_0_0_0_3%]) _
) As [%$TOPIC/bb548659_en-us_VS_110_2_0_0_0_4%]
public static [%$TOPIC/bb548659_en-us_VS_110_2_0_1_0_0%] Max<TSource>(
	this [%$TOPIC/bb548659_en-us_VS_110_2_0_1_0_1%]<TSource> source,
	[%$TOPIC/bb548659_en-us_VS_110_2_0_1_0_2%]<TSource, [%$TOPIC/bb548659_en-us_VS_110_2_0_1_0_3%]> selector
generic<typename TSource>
static [%$TOPIC/bb548659_en-us_VS_110_2_0_2_0_1%] Max(
	[%$TOPIC/bb548659_en-us_VS_110_2_0_2_0_2%]<TSource>^ source, 
	[%$TOPIC/bb548659_en-us_VS_110_2_0_2_0_3%]<TSource, [%$TOPIC/bb548659_en-us_VS_110_2_0_2_0_4%]>^ selector
static member Max : 
        source:[%$TOPIC/bb548659_en-us_VS_110_2_0_3_0_0%]<'TSource> * 
        selector:[%$TOPIC/bb548659_en-us_VS_110_2_0_3_0_1%]<'TSource, [%$TOPIC/bb548659_en-us_VS_110_2_0_3_0_2%]> -> [%$TOPIC/bb548659_en-us_VS_110_2_0_3_0_3%]
JScript does not support generic types or methods.

Type Parameters


The type of the elements of source.


Type: System.Collections.GenericIEnumerableTSource

A sequence of values to determine the maximum value of.

Type: SystemFuncTSource, Decimal

A transform function to apply to each element.

Return Value

Type: SystemDecimal
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 .


source contains no elements.


The MaxTSource(IEnumerableTSource, FuncTSource, Decimal) method uses the Decimal implementation of IComparableT to compare values.

You can apply this method to a sequence of arbitrary values if you provide a function, selector, that projects the members of source into a numeric type, specifically Decimal.

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

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8