Enumerable.Max Método
Definición
Devuelve el valor máximo de una secuencia de valores.Returns the maximum value in a sequence of values.
Sobrecargas
Max(IEnumerable<Nullable<Single>>) |
Devuelve el valor máximo de una secuencia de valores Single que aceptan valores NULL.Returns the maximum value in a sequence of nullable Single values. |
Max(IEnumerable<Int32>) |
Devuelve el valor máximo de una secuencia de valores Int32.Returns the maximum value in a sequence of Int32 values. |
Max(IEnumerable<Nullable<Int64>>) |
Devuelve el valor máximo de una secuencia de valores Int64 que aceptan valores NULL.Returns the maximum value in a sequence of nullable Int64 values. |
Max(IEnumerable<Single>) |
Devuelve el valor máximo de una secuencia de valores Single.Returns the maximum value in a sequence of Single values. |
Max(IEnumerable<Nullable<Int32>>) |
Devuelve el valor máximo de una secuencia de valores Int32 que aceptan valores NULL.Returns the maximum value in a sequence of nullable Int32 values. |
Max(IEnumerable<Decimal>) |
Devuelve el valor máximo de una secuencia de valores Decimal.Returns the maximum value in a sequence of Decimal values. |
Max(IEnumerable<Nullable<Decimal>>) |
Devuelve el valor máximo de una secuencia de valores Decimal que aceptan valores NULL.Returns the maximum value in a sequence of nullable Decimal values. |
Max(IEnumerable<Int64>) |
Devuelve el valor máximo de una secuencia de valores Int64.Returns the maximum value in a sequence of Int64 values. |
Max(IEnumerable<Double>) |
Devuelve el valor máximo de una secuencia de valores Double.Returns the maximum value in a sequence of Double values. |
Max(IEnumerable<Nullable<Double>>) |
Devuelve el valor máximo de una secuencia de valores Double que aceptan valores NULL.Returns the maximum value in a sequence of nullable Double values. |
Max<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>) |
Invoca una función de transformación en cada elemento de una secuencia genérica y devuelve el valor máximo resultante.Invokes a transform function on each element of a generic sequence and returns the maximum resulting value. |
Max<TSource>(IEnumerable<TSource>, Func<TSource,Single>) |
Invoca una función de transformación en cada elemento de una secuencia y devuelve el valor Single máximo.Invokes a transform function on each element of a sequence and returns the maximum Single value. |
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>) |
Invoca una función de transformación en cada elemento de una secuencia y devuelve el valor Single máximo que acepta valores NULL.Invokes a transform function on each element of a sequence and returns the maximum nullable Single value. |
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>) |
Invoca una función de transformación en cada elemento de una secuencia y devuelve el valor Int64 máximo que acepta valores NULL.Invokes a transform function on each element of a sequence and returns the maximum nullable Int64 value. |
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>) |
Invoca una función de transformación en cada elemento de una secuencia y devuelve el valor Int32 máximo que acepta valores NULL.Invokes a transform function on each element of a sequence and returns the maximum nullable Int32 value. |
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>) |
Invoca una función de transformación en cada elemento de una secuencia y devuelve el valor Double máximo que acepta valores NULL.Invokes a transform function on each element of a sequence and returns the maximum nullable Double value. |
Max<TSource>(IEnumerable<TSource>, Func<TSource,Int64>) |
Invoca una función de transformación en cada elemento de una secuencia y devuelve el valor Int64 máximo.Invokes a transform function on each element of a sequence and returns the maximum Int64 value. |
Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) |
Invoca una función de transformación en cada elemento de una secuencia y devuelve el valor Int32 máximo.Invokes a transform function on each element of a sequence and returns the maximum Int32 value. |
Max<TSource>(IEnumerable<TSource>, Func<TSource,Double>) |
Invoca una función de transformación en cada elemento de una secuencia y devuelve el valor Double máximo.Invokes a transform function on each element of a sequence and returns the maximum Double value. |
Max<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>) |
Invoca una función de transformación en cada elemento de una secuencia y devuelve el valor Decimal máximo.Invokes a transform function on each element of a sequence and returns the maximum Decimal value. |
Max<TSource>(IEnumerable<TSource>) |
Devuelve el valor máximo de una secuencia genérica.Returns the maximum value in a generic sequence. |
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>) |
Invoca una función de transformación en cada elemento de una secuencia y devuelve el valor Decimal máximo que acepta valores NULL.Invokes a transform function on each element of a sequence and returns the maximum nullable Decimal value. |
Max(IEnumerable<Nullable<Single>>)
public:
[System::Runtime::CompilerServices::Extension]
static Nullable<float> Max(System::Collections::Generic::IEnumerable<Nullable<float>> ^ source);
public static float? Max (this System.Collections.Generic.IEnumerable<Nullable<float>> source);
static member Max : seq<Nullable<single>> -> Nullable<single>
<Extension()>
Public Function Max (source As IEnumerable(Of Nullable(Of Single))) As Nullable(Of Single)
Parámetros
- source
- IEnumerable<Nullable<Single>>
Secuencia de valores Single que aceptan valores NULL cuyo valor máximo se va a determinar.A sequence of nullable Single values to determine the maximum value of.
Devoluciones
Valor de tipo Nullable<Single>
en C# o Nullable(Of Single)
en Visual Basic que corresponde al valor máximo de la secuencia.A value of type Nullable<Single>
in C# or Nullable(Of Single)
in Visual Basic that corresponds to the maximum value in the sequence.
Excepciones
source
es null
.source
is null
.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max(IEnumerable<Nullable<Double>>) para determinar el valor máximo de una secuencia.The following code example demonstrates how to use Max(IEnumerable<Nullable<Double>>) to determine the maximum value in a sequence.
Nota
En este ejemplo de código se usa una sobrecarga de este método sobrecargado diferente de la carga específica que se describe en este tema.This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. Para ampliar el ejemplo a este tema, sustituye los elementos de la secuencia de origen por los elementos del tipo numérico adecuado.To extend the example to this topic, substitute the elements of the source sequence with elements of the appropriate numerical type.
double?[] doubles = { null, 1.5E+104, 9E+103, -2E+103 };
double? max = doubles.Max();
Console.WriteLine("The largest number is {0}.", max);
/*
This code produces the following output:
The largest number is 1.5E+104.
*/
' Create an array of Nullable Double values.
Dim doubles() As Nullable(Of Double) =
{Nothing, 1.5E+104, 9.0E+103, -2.0E+103}
' Determine the maximum value in the array.
Dim max As Nullable(Of Double) = doubles.Max()
' Display the result.
Console.WriteLine($"The largest number is {max}")
' This code produces the following output:
'
' The largest number is 1.5E+104
Comentarios
El Max(IEnumerable<Nullable<Single>>) método utiliza la Single implementación de IComparable<T> para comparar los valores.The Max(IEnumerable<Nullable<Single>>) method uses the Single implementation of IComparable<T> to compare values.
Si la secuencia de origen está vacía o contiene solo valores que son null
, esta función devuelve null
.If the source sequence is empty or contains only values that are null
, this function returns null
.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max(IEnumerable<Int32>)
public:
[System::Runtime::CompilerServices::Extension]
static int Max(System::Collections::Generic::IEnumerable<int> ^ source);
public static int Max (this System.Collections.Generic.IEnumerable<int> source);
static member Max : seq<int> -> int
<Extension()>
Public Function Max (source As IEnumerable(Of Integer)) As Integer
Parámetros
- source
- IEnumerable<Int32>
Secuencia de valores Int32 cuyo valor máximo se va a determinar.A sequence of Int32 values to determine the maximum value of.
Devoluciones
El valor máximo de la secuencia.The maximum value in the sequence.
Excepciones
source
es null
.source
is null
.
source
no contiene ningún elemento.source
contains no elements.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max(IEnumerable<Int64>) para determinar el valor máximo de una secuencia.The following code example demonstrates how to use Max(IEnumerable<Int64>) to determine the maximum value in a sequence.
Nota
En este ejemplo de código se usa una sobrecarga de este método sobrecargado diferente de la carga específica que se describe en este tema.This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. Para ampliar el ejemplo a este tema, sustituye los elementos de la secuencia de origen por los elementos del tipo numérico adecuado.To extend the example to this topic, substitute the elements of the source sequence with elements of the appropriate numerical type.
List<long> longs = new List<long> { 4294967296L, 466855135L, 81125L };
long max = longs.Max();
Console.WriteLine("The largest number is {0}.", max);
/*
This code produces the following output:
The largest number is 4294967296.
*/
' Create a list of Long values.
Dim longs As New List(Of Long)(New Long() _
{4294967296L, 466855135L, 81125L})
' Get the maximum value in the list.
Dim max As Long = longs.Max()
' Display the result.
Console.WriteLine($"The largest number is {max}")
' This code produces the following output:
'
' The largest number is 4294967296
Comentarios
El Max(IEnumerable<Int32>) método utiliza la Int32 implementación de IComparable<T> para comparar los valores.The Max(IEnumerable<Int32>) method uses the Int32 implementation of IComparable<T> to compare values.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max(IEnumerable<Nullable<Int64>>)
public:
[System::Runtime::CompilerServices::Extension]
static Nullable<long> Max(System::Collections::Generic::IEnumerable<Nullable<long>> ^ source);
public static long? Max (this System.Collections.Generic.IEnumerable<Nullable<long>> source);
static member Max : seq<Nullable<int64>> -> Nullable<int64>
<Extension()>
Public Function Max (source As IEnumerable(Of Nullable(Of Long))) As Nullable(Of Long)
Parámetros
- source
- IEnumerable<Nullable<Int64>>
Secuencia de valores Int64 que aceptan valores NULL cuyo valor máximo se va a determinar.A sequence of nullable Int64 values to determine the maximum value of.
Devoluciones
Valor de tipo Nullable<Int64>
en C# o Nullable(Of Int64)
en Visual Basic que corresponde al valor máximo de la secuencia.A value of type Nullable<Int64>
in C# or Nullable(Of Int64)
in Visual Basic that corresponds to the maximum value in the sequence.
Excepciones
source
es null
.source
is null
.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max(IEnumerable<Nullable<Double>>) para determinar el valor máximo de una secuencia.The following code example demonstrates how to use Max(IEnumerable<Nullable<Double>>) to determine the maximum value in a sequence.
Nota
En este ejemplo de código se usa una sobrecarga de este método sobrecargado diferente de la carga específica que se describe en este tema.This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. Para ampliar el ejemplo a este tema, sustituye los elementos de la secuencia de origen por los elementos del tipo numérico adecuado.To extend the example to this topic, substitute the elements of the source sequence with elements of the appropriate numerical type.
double?[] doubles = { null, 1.5E+104, 9E+103, -2E+103 };
double? max = doubles.Max();
Console.WriteLine("The largest number is {0}.", max);
/*
This code produces the following output:
The largest number is 1.5E+104.
*/
' Create an array of Nullable Double values.
Dim doubles() As Nullable(Of Double) =
{Nothing, 1.5E+104, 9.0E+103, -2.0E+103}
' Determine the maximum value in the array.
Dim max As Nullable(Of Double) = doubles.Max()
' Display the result.
Console.WriteLine($"The largest number is {max}")
' This code produces the following output:
'
' The largest number is 1.5E+104
Comentarios
El Max(IEnumerable<Nullable<Int64>>) método utiliza la Int64 implementación de IComparable<T> para comparar los valores.The Max(IEnumerable<Nullable<Int64>>) method uses the Int64 implementation of IComparable<T> to compare values.
Si la secuencia de origen está vacía o contiene solo valores que son null
, esta función devuelve null
.If the source sequence is empty or contains only values that are null
, this function returns null
.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max(IEnumerable<Single>)
public:
[System::Runtime::CompilerServices::Extension]
static float Max(System::Collections::Generic::IEnumerable<float> ^ source);
public static float Max (this System.Collections.Generic.IEnumerable<float> source);
static member Max : seq<single> -> single
<Extension()>
Public Function Max (source As IEnumerable(Of Single)) As Single
Parámetros
- source
- IEnumerable<Single>
Secuencia de valores Single cuyo valor máximo se va a determinar.A sequence of Single values to determine the maximum value of.
Devoluciones
El valor máximo de la secuencia.The maximum value in the sequence.
Excepciones
source
es null
.source
is null
.
source
no contiene ningún elemento.source
contains no elements.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max(IEnumerable<Int64>) para determinar el valor máximo de una secuencia.The following code example demonstrates how to use Max(IEnumerable<Int64>) to determine the maximum value in a sequence.
Nota
En este ejemplo de código se usa una sobrecarga de este método sobrecargado diferente de la carga específica que se describe en este tema.This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. Para ampliar el ejemplo a este tema, sustituye los elementos de la secuencia de origen por los elementos del tipo numérico adecuado.To extend the example to this topic, substitute the elements of the source sequence with elements of the appropriate numerical type.
List<long> longs = new List<long> { 4294967296L, 466855135L, 81125L };
long max = longs.Max();
Console.WriteLine("The largest number is {0}.", max);
/*
This code produces the following output:
The largest number is 4294967296.
*/
' Create a list of Long values.
Dim longs As New List(Of Long)(New Long() _
{4294967296L, 466855135L, 81125L})
' Get the maximum value in the list.
Dim max As Long = longs.Max()
' Display the result.
Console.WriteLine($"The largest number is {max}")
' This code produces the following output:
'
' The largest number is 4294967296
Comentarios
El Max(IEnumerable<Single>) método utiliza la Single implementación de IComparable<T> para comparar los valores.The Max(IEnumerable<Single>) method uses the Single implementation of IComparable<T> to compare values.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max(IEnumerable<Nullable<Int32>>)
public:
[System::Runtime::CompilerServices::Extension]
static Nullable<int> Max(System::Collections::Generic::IEnumerable<Nullable<int>> ^ source);
public static int? Max (this System.Collections.Generic.IEnumerable<Nullable<int>> source);
static member Max : seq<Nullable<int>> -> Nullable<int>
<Extension()>
Public Function Max (source As IEnumerable(Of Nullable(Of Integer))) As Nullable(Of Integer)
Parámetros
- source
- IEnumerable<Nullable<Int32>>
Secuencia de valores Int32 que aceptan valores NULL cuyo valor máximo se va a determinar.A sequence of nullable Int32 values to determine the maximum value of.
Devoluciones
Valor de tipo Nullable<Int32>
en C# o Nullable(Of Int32)
en Visual Basic que corresponde al valor máximo de la secuencia.A value of type Nullable<Int32>
in C# or Nullable(Of Int32)
in Visual Basic that corresponds to the maximum value in the sequence.
Excepciones
source
es null
.source
is null
.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max(IEnumerable<Nullable<Double>>) para determinar el valor máximo de una secuencia.The following code example demonstrates how to use Max(IEnumerable<Nullable<Double>>) to determine the maximum value in a sequence.
Nota
En este ejemplo de código se usa una sobrecarga de este método sobrecargado diferente de la carga específica que se describe en este tema.This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. Para ampliar el ejemplo a este tema, sustituye los elementos de la secuencia de origen por los elementos del tipo numérico adecuado.To extend the example to this topic, substitute the elements of the source sequence with elements of the appropriate numerical type.
double?[] doubles = { null, 1.5E+104, 9E+103, -2E+103 };
double? max = doubles.Max();
Console.WriteLine("The largest number is {0}.", max);
/*
This code produces the following output:
The largest number is 1.5E+104.
*/
' Create an array of Nullable Double values.
Dim doubles() As Nullable(Of Double) =
{Nothing, 1.5E+104, 9.0E+103, -2.0E+103}
' Determine the maximum value in the array.
Dim max As Nullable(Of Double) = doubles.Max()
' Display the result.
Console.WriteLine($"The largest number is {max}")
' This code produces the following output:
'
' The largest number is 1.5E+104
Comentarios
El Max(IEnumerable<Nullable<Int32>>) método utiliza la Int32 implementación de IComparable<T> para comparar los valores.The Max(IEnumerable<Nullable<Int32>>) method uses the Int32 implementation of IComparable<T> to compare values.
Si la secuencia de origen está vacía o contiene solo valores que son null
, esta función devuelve null
.If the source sequence is empty or contains only values that are null
, this function returns null
.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max(IEnumerable<Decimal>)
public:
[System::Runtime::CompilerServices::Extension]
static System::Decimal Max(System::Collections::Generic::IEnumerable<System::Decimal> ^ source);
public static decimal Max (this System.Collections.Generic.IEnumerable<decimal> source);
static member Max : seq<decimal> -> decimal
<Extension()>
Public Function Max (source As IEnumerable(Of Decimal)) As Decimal
Parámetros
- source
- IEnumerable<Decimal>
Secuencia de valores Decimal cuyo valor máximo se va a determinar.A sequence of Decimal values to determine the maximum value of.
Devoluciones
El valor máximo de la secuencia.The maximum value in the sequence.
Excepciones
source
es null
.source
is null
.
source
no contiene ningún elemento.source
contains no elements.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max(IEnumerable<Int64>) para determinar el valor máximo de una secuencia.The following code example demonstrates how to use Max(IEnumerable<Int64>) to determine the maximum value in a sequence.
Nota
En este ejemplo de código se usa una sobrecarga de este método sobrecargado diferente de la carga específica que se describe en este tema.This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. Para ampliar el ejemplo a este tema, sustituye los elementos de la secuencia de origen por los elementos del tipo numérico adecuado.To extend the example to this topic, substitute the elements of the source sequence with elements of the appropriate numerical type.
List<long> longs = new List<long> { 4294967296L, 466855135L, 81125L };
long max = longs.Max();
Console.WriteLine("The largest number is {0}.", max);
/*
This code produces the following output:
The largest number is 4294967296.
*/
' Create a list of Long values.
Dim longs As New List(Of Long)(New Long() _
{4294967296L, 466855135L, 81125L})
' Get the maximum value in the list.
Dim max As Long = longs.Max()
' Display the result.
Console.WriteLine($"The largest number is {max}")
' This code produces the following output:
'
' The largest number is 4294967296
Comentarios
El Max(IEnumerable<Decimal>) método utiliza la Decimal implementación de IComparable<T> para comparar los valores.The Max(IEnumerable<Decimal>) method uses the Decimal implementation of IComparable<T> to compare values.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max(IEnumerable<Nullable<Decimal>>)
public:
[System::Runtime::CompilerServices::Extension]
static Nullable<System::Decimal> Max(System::Collections::Generic::IEnumerable<Nullable<System::Decimal>> ^ source);
public static decimal? Max (this System.Collections.Generic.IEnumerable<Nullable<decimal>> source);
static member Max : seq<Nullable<decimal>> -> Nullable<decimal>
<Extension()>
Public Function Max (source As IEnumerable(Of Nullable(Of Decimal))) As Nullable(Of Decimal)
Parámetros
- source
- IEnumerable<Nullable<Decimal>>
Secuencia de valores Decimal que aceptan valores NULL cuyo valor máximo se va a determinar.A sequence of nullable Decimal values to determine the maximum value of.
Devoluciones
Valor de tipo Nullable<Decimal>
en C# o Nullable(Of Decimal)
en Visual Basic que corresponde al valor máximo de la secuencia.A value of type Nullable<Decimal>
in C# or Nullable(Of Decimal)
in Visual Basic that corresponds to the maximum value in the sequence.
Excepciones
source
es null
.source
is null
.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max(IEnumerable<Nullable<Double>>) para determinar el valor máximo de una secuencia.The following code example demonstrates how to use Max(IEnumerable<Nullable<Double>>) to determine the maximum value in a sequence.
Nota
En este ejemplo de código se usa una sobrecarga de este método sobrecargado diferente de la carga específica que se describe en este tema.This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. Para ampliar el ejemplo a este tema, sustituye los elementos de la secuencia de origen por los elementos del tipo numérico adecuado.To extend the example to this topic, substitute the elements of the source sequence with elements of the appropriate numerical type.
double?[] doubles = { null, 1.5E+104, 9E+103, -2E+103 };
double? max = doubles.Max();
Console.WriteLine("The largest number is {0}.", max);
/*
This code produces the following output:
The largest number is 1.5E+104.
*/
' Create an array of Nullable Double values.
Dim doubles() As Nullable(Of Double) =
{Nothing, 1.5E+104, 9.0E+103, -2.0E+103}
' Determine the maximum value in the array.
Dim max As Nullable(Of Double) = doubles.Max()
' Display the result.
Console.WriteLine($"The largest number is {max}")
' This code produces the following output:
'
' The largest number is 1.5E+104
Comentarios
El Max(IEnumerable<Nullable<Decimal>>) método utiliza la Decimal implementación de IComparable<T> para comparar los valores.The Max(IEnumerable<Nullable<Decimal>>) method uses the Decimal implementation of IComparable<T> to compare values.
Si la secuencia de origen está vacía o contiene solo valores que son null
, esta función devuelve null
.If the source sequence is empty or contains only values that are null
, this function returns null
.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max(IEnumerable<Int64>)
public:
[System::Runtime::CompilerServices::Extension]
static long Max(System::Collections::Generic::IEnumerable<long> ^ source);
public static long Max (this System.Collections.Generic.IEnumerable<long> source);
static member Max : seq<int64> -> int64
<Extension()>
Public Function Max (source As IEnumerable(Of Long)) As Long
Parámetros
- source
- IEnumerable<Int64>
Secuencia de valores Int64 cuyo valor máximo se va a determinar.A sequence of Int64 values to determine the maximum value of.
Devoluciones
El valor máximo de la secuencia.The maximum value in the sequence.
Excepciones
source
es null
.source
is null
.
source
no contiene ningún elemento.source
contains no elements.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max(IEnumerable<Int64>) para determinar el valor máximo de una secuencia.The following code example demonstrates how to use Max(IEnumerable<Int64>) to determine the maximum value in a sequence.
List<long> longs = new List<long> { 4294967296L, 466855135L, 81125L };
long max = longs.Max();
Console.WriteLine("The largest number is {0}.", max);
/*
This code produces the following output:
The largest number is 4294967296.
*/
' Create a list of Long values.
Dim longs As New List(Of Long)(New Long() _
{4294967296L, 466855135L, 81125L})
' Get the maximum value in the list.
Dim max As Long = longs.Max()
' Display the result.
Console.WriteLine($"The largest number is {max}")
' This code produces the following output:
'
' The largest number is 4294967296
Comentarios
El Max(IEnumerable<Int64>) método utiliza la Int64 implementación de IComparable<T> para comparar los valores.The Max(IEnumerable<Int64>) method uses the Int64 implementation of IComparable<T> to compare values.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max(IEnumerable<Double>)
public:
[System::Runtime::CompilerServices::Extension]
static double Max(System::Collections::Generic::IEnumerable<double> ^ source);
public static double Max (this System.Collections.Generic.IEnumerable<double> source);
static member Max : seq<double> -> double
<Extension()>
Public Function Max (source As IEnumerable(Of Double)) As Double
Parámetros
- source
- IEnumerable<Double>
Secuencia de valores Double cuyo valor máximo se va a determinar.A sequence of Double values to determine the maximum value of.
Devoluciones
El valor máximo de la secuencia.The maximum value in the sequence.
Excepciones
source
es null
.source
is null
.
source
no contiene ningún elemento.source
contains no elements.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max(IEnumerable<Int64>) para determinar el valor máximo de una secuencia.The following code example demonstrates how to use Max(IEnumerable<Int64>) to determine the maximum value in a sequence.
Nota
En este ejemplo de código se usa una sobrecarga de este método sobrecargado diferente de la carga específica que se describe en este tema.This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. Para ampliar el ejemplo a este tema, sustituye los elementos de la secuencia de origen por los elementos del tipo numérico adecuado.To extend the example to this topic, substitute the elements of the source sequence with elements of the appropriate numerical type.
List<long> longs = new List<long> { 4294967296L, 466855135L, 81125L };
long max = longs.Max();
Console.WriteLine("The largest number is {0}.", max);
/*
This code produces the following output:
The largest number is 4294967296.
*/
' Create a list of Long values.
Dim longs As New List(Of Long)(New Long() _
{4294967296L, 466855135L, 81125L})
' Get the maximum value in the list.
Dim max As Long = longs.Max()
' Display the result.
Console.WriteLine($"The largest number is {max}")
' This code produces the following output:
'
' The largest number is 4294967296
Comentarios
El Max(IEnumerable<Double>) método utiliza la Double implementación de IComparable<T> para comparar los valores.The Max(IEnumerable<Double>) method uses the Double implementation of IComparable<T> to compare values.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max(IEnumerable<Nullable<Double>>)
public:
[System::Runtime::CompilerServices::Extension]
static Nullable<double> Max(System::Collections::Generic::IEnumerable<Nullable<double>> ^ source);
public static double? Max (this System.Collections.Generic.IEnumerable<Nullable<double>> source);
static member Max : seq<Nullable<double>> -> Nullable<double>
<Extension()>
Public Function Max (source As IEnumerable(Of Nullable(Of Double))) As Nullable(Of Double)
Parámetros
- source
- IEnumerable<Nullable<Double>>
Secuencia de valores Double que aceptan valores NULL cuyo valor máximo se va a determinar.A sequence of nullable Double values to determine the maximum value of.
Devoluciones
Valor de tipo Nullable<Double>
en C# o Nullable(Of Double)
en Visual Basic que corresponde al valor máximo de la secuencia.A value of type Nullable<Double>
in C# or Nullable(Of Double)
in Visual Basic that corresponds to the maximum value in the sequence.
Excepciones
source
es null
.source
is null
.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max(IEnumerable<Nullable<Double>>) para determinar el valor máximo de una secuencia.The following code example demonstrates how to use Max(IEnumerable<Nullable<Double>>) to determine the maximum value in a sequence.
double?[] doubles = { null, 1.5E+104, 9E+103, -2E+103 };
double? max = doubles.Max();
Console.WriteLine("The largest number is {0}.", max);
/*
This code produces the following output:
The largest number is 1.5E+104.
*/
' Create an array of Nullable Double values.
Dim doubles() As Nullable(Of Double) =
{Nothing, 1.5E+104, 9.0E+103, -2.0E+103}
' Determine the maximum value in the array.
Dim max As Nullable(Of Double) = doubles.Max()
' Display the result.
Console.WriteLine($"The largest number is {max}")
' This code produces the following output:
'
' The largest number is 1.5E+104
Comentarios
El Max(IEnumerable<Nullable<Double>>) método utiliza la Double implementación de IComparable<T> para comparar los valores.The Max(IEnumerable<Nullable<Double>>) method uses the Double implementation of IComparable<T> to compare values.
Si la secuencia de origen está vacía o contiene solo valores que son null
, esta función devuelve null
.If the source sequence is empty or contains only values that are null
, this function returns null
.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>)
Invoca una función de transformación en cada elemento de una secuencia genérica y devuelve el valor máximo resultante.Invokes a transform function on each element of a generic sequence and returns the maximum resulting value.
public:
generic <typename TSource, typename TResult>
[System::Runtime::CompilerServices::Extension]
static TResult Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TResult> ^ selector);
public static TResult Max<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TResult> selector);
public static TResult Max<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource>? source, Func<TSource,TResult>? selector);
static member Max : seq<'Source> * Func<'Source, 'Result> -> 'Result
<Extension()>
Public Function Max(Of TSource, TResult) (source As IEnumerable(Of TSource), selector As Func(Of TSource, TResult)) As TResult
Parámetros de tipo
- TSource
Tipo de los elementos de source
.The type of the elements of source
.
- TResult
Tipo de valor devuelto por selector
.The type of the value returned by selector
.
Parámetros
- source
- IEnumerable<TSource>
Secuencia de valores cuyo valor máximo se va a determinar.A sequence of values to determine the maximum value of.
- selector
- Func<TSource,TResult>
Función de transformación que se va a aplicar a cada elemento.A transform function to apply to each element.
Devoluciones
- TResult
El valor máximo de la secuencia.The maximum value in the sequence.
Excepciones
source
o selector
es null
.source
or selector
is null
.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) para determinar el valor máximo de una secuencia de valores proyectados.The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the maximum value in a sequence of projected values.
Nota
En este ejemplo de código se usa una sobrecarga de este método sobrecargado diferente de la carga específica que se describe en este tema.This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. Para ampliar el ejemplo a este tema, cambie el cuerpo de la función selector
.To extend the example to this topic, change the body of the selector
function.
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);
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
The maximum pet age plus name length is 14.
*/
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.
Console.WriteLine($"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
Comentarios
Si TResult
el tipo implementa IComparable<T> , este método utiliza esa implementación para comparar los valores.If type TResult
implements IComparable<T>, this method uses that implementation to compare values. De lo contrario, si el tipo TResult
implementa IComparable , esa implementación se utiliza para comparar los valores.Otherwise, if type TResult
implements IComparable, that implementation is used to compare values.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max<TSource>(IEnumerable<TSource>, Func<TSource,Single>)
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static float Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, float> ^ selector);
public static float Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,float> selector);
static member Max : seq<'Source> * Func<'Source, single> -> single
<Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Single)) As Single
Parámetros de tipo
- TSource
Tipo de los elementos de source
.The type of the elements of source
.
Parámetros
- source
- IEnumerable<TSource>
Secuencia de valores cuyo valor máximo se va a determinar.A sequence of values to determine the maximum value of.
Función de transformación que se va a aplicar a cada elemento.A transform function to apply to each element.
Devoluciones
El valor máximo de la secuencia.The maximum value in the sequence.
Excepciones
source
o selector
es null
.source
or selector
is null
.
source
no contiene ningún elemento.source
contains no elements.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) para determinar el valor máximo de una secuencia de valores proyectados.The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the maximum value in a sequence of projected values.
Nota
En este ejemplo de código se usa una sobrecarga de este método sobrecargado diferente de la carga específica que se describe en este tema.This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. Para ampliar el ejemplo a este tema, cambie el cuerpo de la función selector
.To extend the example to this topic, change the body of the selector
function.
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);
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
The maximum pet age plus name length is 14.
*/
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.
Console.WriteLine($"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
Comentarios
El Max<TSource>(IEnumerable<TSource>, Func<TSource,Single>) método utiliza la Single implementación de IComparable<T> para comparar los valores.The Max<TSource>(IEnumerable<TSource>, Func<TSource,Single>) method uses the Single implementation of IComparable<T> to compare values.
Este método se puede aplicar a una secuencia de valores arbitrarios si se proporciona una función, selector
, que proyecta los miembros de source
en un tipo numérico, en concreto Single .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 Single.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static Nullable<float> Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<float>> ^ selector);
public static float? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,Nullable<float>> selector);
static member Max : seq<'Source> * Func<'Source, Nullable<single>> -> Nullable<single>
<Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Single))) As Nullable(Of Single)
Parámetros de tipo
- TSource
Tipo de los elementos de source
.The type of the elements of source
.
Parámetros
- source
- IEnumerable<TSource>
Secuencia de valores cuyo valor máximo se va a determinar.A sequence of values to determine the maximum value of.
Función de transformación que se va a aplicar a cada elemento.A transform function to apply to each element.
Devoluciones
Valor de tipo Nullable<Single>
en C# o Nullable(Of Single)
en Visual Basic que corresponde al valor máximo de la secuencia.The value of type Nullable<Single>
in C# or Nullable(Of Single)
in Visual Basic that corresponds to the maximum value in the sequence.
Excepciones
source
o selector
es null
.source
or selector
is null
.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) para determinar el valor máximo de una secuencia de valores proyectados.The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the maximum value in a sequence of projected values.
Nota
En este ejemplo de código se usa una sobrecarga de este método sobrecargado diferente de la carga específica que se describe en este tema.This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. Para ampliar el ejemplo a este tema, cambie el cuerpo de la función selector
.To extend the example to this topic, change the body of the selector
function.
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);
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
The maximum pet age plus name length is 14.
*/
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.
Console.WriteLine($"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
Comentarios
El Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>) método utiliza la Single implementación de IComparable<T> para comparar los valores.The Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>) method uses the Single implementation of IComparable<T> to compare values.
Este método se puede aplicar a una secuencia de valores arbitrarios si se proporciona una función, selector
, que proyecta los miembros de source
en un tipo numérico, específicamente Nullable<Single>
en C# o Nullable(Of Single)
en Visual Basic.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 Nullable<Single>
in C# or Nullable(Of Single)
in Visual Basic.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static Nullable<long> Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<long>> ^ selector);
public static long? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,Nullable<long>> selector);
static member Max : seq<'Source> * Func<'Source, Nullable<int64>> -> Nullable<int64>
<Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Long))) As Nullable(Of Long)
Parámetros de tipo
- TSource
Tipo de los elementos de source
.The type of the elements of source
.
Parámetros
- source
- IEnumerable<TSource>
Secuencia de valores cuyo valor máximo se va a determinar.A sequence of values to determine the maximum value of.
Función de transformación que se va a aplicar a cada elemento.A transform function to apply to each element.
Devoluciones
Valor de tipo Nullable<Int64>
en C# o Nullable(Of Int64)
en Visual Basic que corresponde al valor máximo de la secuencia.The value of type Nullable<Int64>
in C# or Nullable(Of Int64)
in Visual Basic that corresponds to the maximum value in the sequence.
Excepciones
source
o selector
es null
.source
or selector
is null
.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) para determinar el valor máximo de una secuencia de valores proyectados.The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the maximum value in a sequence of projected values.
Nota
En este ejemplo de código se usa una sobrecarga de este método sobrecargado diferente de la carga específica que se describe en este tema.This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. Para ampliar el ejemplo a este tema, cambie el cuerpo de la función selector
.To extend the example to this topic, change the body of the selector
function.
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);
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
The maximum pet age plus name length is 14.
*/
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.
Console.WriteLine($"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
Comentarios
El Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>) método utiliza la Int64 implementación de IComparable<T> para comparar los valores.The Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>) method uses the Int64 implementation of IComparable<T> to compare values.
Este método se puede aplicar a una secuencia de valores arbitrarios si se proporciona una función, selector
, que proyecta los miembros de source
en un tipo numérico, específicamente Nullable<Int64>
en C# o Nullable(Of Int64)
en Visual Basic.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 Nullable<Int64>
in C# or Nullable(Of Int64)
in Visual Basic.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static Nullable<int> Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<int>> ^ selector);
public static int? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,Nullable<int>> selector);
static member Max : seq<'Source> * Func<'Source, Nullable<int>> -> Nullable<int>
<Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Integer))) As Nullable(Of Integer)
Parámetros de tipo
- TSource
Tipo de los elementos de source
.The type of the elements of source
.
Parámetros
- source
- IEnumerable<TSource>
Secuencia de valores cuyo valor máximo se va a determinar.A sequence of values to determine the maximum value of.
Función de transformación que se va a aplicar a cada elemento.A transform function to apply to each element.
Devoluciones
Valor de tipo Nullable<Int32>
en C# o Nullable(Of Int32)
en Visual Basic que corresponde al valor máximo de la secuencia.The value of type Nullable<Int32>
in C# or Nullable(Of Int32)
in Visual Basic that corresponds to the maximum value in the sequence.
Excepciones
source
o selector
es null
.source
or selector
is null
.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) para determinar el valor máximo de una secuencia de valores proyectados.The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the maximum value in a sequence of projected values.
Nota
En este ejemplo de código se usa una sobrecarga de este método sobrecargado diferente de la carga específica que se describe en este tema.This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. Para ampliar el ejemplo a este tema, cambie el cuerpo de la función selector
.To extend the example to this topic, change the body of the selector
function.
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);
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
The maximum pet age plus name length is 14.
*/
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.
Console.WriteLine($"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
Comentarios
El Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>) método utiliza la Int32 implementación de IComparable<T> para comparar los valores.The Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>) method uses the Int32 implementation of IComparable<T> to compare values.
Este método se puede aplicar a una secuencia de valores arbitrarios si se proporciona una función, selector
, que proyecta los miembros de source
en un tipo numérico, específicamente Nullable<Int32>
en C# o Nullable(Of Int32)
en Visual Basic.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 Nullable<Int32>
in C# or Nullable(Of Int32)
in Visual Basic.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static Nullable<double> Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<double>> ^ selector);
public static double? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,Nullable<double>> selector);
static member Max : seq<'Source> * Func<'Source, Nullable<double>> -> Nullable<double>
<Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Double))) As Nullable(Of Double)
Parámetros de tipo
- TSource
Tipo de los elementos de source
.The type of the elements of source
.
Parámetros
- source
- IEnumerable<TSource>
Secuencia de valores cuyo valor máximo se va a determinar.A sequence of values to determine the maximum value of.
Función de transformación que se va a aplicar a cada elemento.A transform function to apply to each element.
Devoluciones
Valor de tipo Nullable<Double>
en C# o Nullable(Of Double)
en Visual Basic que corresponde al valor máximo de la secuencia.The value of type Nullable<Double>
in C# or Nullable(Of Double)
in Visual Basic that corresponds to the maximum value in the sequence.
Excepciones
source
o selector
es null
.source
or selector
is null
.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) para determinar el valor máximo de una secuencia de valores proyectados.The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the maximum value in a sequence of projected values.
Nota
En este ejemplo de código se usa una sobrecarga de este método sobrecargado diferente de la carga específica que se describe en este tema.This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. Para ampliar el ejemplo a este tema, cambie el cuerpo de la función selector
.To extend the example to this topic, change the body of the selector
function.
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);
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
The maximum pet age plus name length is 14.
*/
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.
Console.WriteLine($"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
Comentarios
El Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>) método utiliza la Double implementación de IComparable<T> para comparar los valores.The Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>) method uses the Double implementation of IComparable<T> to compare values.
Este método se puede aplicar a una secuencia de valores arbitrarios si se proporciona una función, selector
, que proyecta los miembros de source
en un tipo numérico, específicamente Nullable<Double>
en C# o Nullable(Of Double)
en Visual Basic.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 Nullable<Double>
in C# or Nullable(Of Double)
in Visual Basic.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static long Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, long> ^ selector);
public static long Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,long> selector);
static member Max : seq<'Source> * Func<'Source, int64> -> int64
<Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Long)) As Long
Parámetros de tipo
- TSource
Tipo de los elementos de source
.The type of the elements of source
.
Parámetros
- source
- IEnumerable<TSource>
Secuencia de valores cuyo valor máximo se va a determinar.A sequence of values to determine the maximum value of.
Función de transformación que se va a aplicar a cada elemento.A transform function to apply to each element.
Devoluciones
El valor máximo de la secuencia.The maximum value in the sequence.
Excepciones
source
o selector
es null
.source
or selector
is null
.
source
no contiene ningún elemento.source
contains no elements.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) para determinar el valor máximo de una secuencia de valores proyectados.The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the maximum value in a sequence of projected values.
Nota
En este ejemplo de código se usa una sobrecarga de este método sobrecargado diferente de la carga específica que se describe en este tema.This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. Para ampliar el ejemplo a este tema, cambie el cuerpo de la función selector
.To extend the example to this topic, change the body of the selector
function.
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);
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
The maximum pet age plus name length is 14.
*/
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.
Console.WriteLine($"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
Comentarios
El Max<TSource>(IEnumerable<TSource>, Func<TSource,Int64>) método utiliza la Int64 implementación de IComparable<T> para comparar los valores.The Max<TSource>(IEnumerable<TSource>, Func<TSource,Int64>) method uses the Int64 implementation of IComparable<T> to compare values.
Este método se puede aplicar a una secuencia de valores arbitrarios si se proporciona una función, selector
, que proyecta los miembros de source
en un tipo numérico, en concreto Int64 .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 Int64.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static int Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, int> ^ selector);
public static int Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int> selector);
static member Max : seq<'Source> * Func<'Source, int> -> int
<Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Integer)) As Integer
Parámetros de tipo
- TSource
Tipo de los elementos de source
.The type of the elements of source
.
Parámetros
- source
- IEnumerable<TSource>
Secuencia de valores cuyo valor máximo se va a determinar.A sequence of values to determine the maximum value of.
Función de transformación que se va a aplicar a cada elemento.A transform function to apply to each element.
Devoluciones
El valor máximo de la secuencia.The maximum value in the sequence.
Excepciones
source
o selector
es null
.source
or selector
is null
.
source
no contiene ningún elemento.source
contains no elements.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) para determinar el valor máximo de una secuencia de valores proyectados.The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the maximum value in a sequence of projected values.
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);
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
The maximum pet age plus name length is 14.
*/
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.
Console.WriteLine($"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
Comentarios
El Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) método utiliza la Int32 implementación de IComparable<T> para comparar los valores.The Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) method uses the Int32 implementation of IComparable<T> to compare values.
Este método se puede aplicar a una secuencia de valores arbitrarios si se proporciona una función, selector
, que proyecta los miembros de source
en un tipo numérico, en concreto Int32 .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 Int32.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max<TSource>(IEnumerable<TSource>, Func<TSource,Double>)
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static double Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, double> ^ selector);
public static double Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,double> selector);
static member Max : seq<'Source> * Func<'Source, double> -> double
<Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Double)) As Double
Parámetros de tipo
- TSource
Tipo de los elementos de source
.The type of the elements of source
.
Parámetros
- source
- IEnumerable<TSource>
Secuencia de valores cuyo valor máximo se va a determinar.A sequence of values to determine the maximum value of.
Función de transformación que se va a aplicar a cada elemento.A transform function to apply to each element.
Devoluciones
El valor máximo de la secuencia.The maximum value in the sequence.
Excepciones
source
o selector
es null
.source
or selector
is null
.
source
no contiene ningún elemento.source
contains no elements.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) para determinar el valor máximo de una secuencia de valores proyectados.The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the maximum value in a sequence of projected values.
Nota
En este ejemplo de código se usa una sobrecarga de este método sobrecargado diferente de la carga específica que se describe en este tema.This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. Para ampliar el ejemplo a este tema, cambie el cuerpo de la función selector
.To extend the example to this topic, change the body of the selector
function.
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);
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
The maximum pet age plus name length is 14.
*/
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.
Console.WriteLine($"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
Comentarios
El Max<TSource>(IEnumerable<TSource>, Func<TSource,Double>) método utiliza la Double implementación de IComparable<T> para comparar los valores.The Max<TSource>(IEnumerable<TSource>, Func<TSource,Double>) method uses the Double implementation of IComparable<T> to compare values.
Este método se puede aplicar a una secuencia de valores arbitrarios si se proporciona una función, selector
, que proyecta los miembros de source
en un tipo numérico, en concreto Double .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 Double.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static System::Decimal Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, System::Decimal> ^ selector);
public static decimal Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,decimal> selector);
static member Max : seq<'Source> * Func<'Source, decimal> -> decimal
<Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Decimal)) As Decimal
Parámetros de tipo
- TSource
Tipo de los elementos de source
.The type of the elements of source
.
Parámetros
- source
- IEnumerable<TSource>
Secuencia de valores cuyo valor máximo se va a determinar.A sequence of values to determine the maximum value of.
Función de transformación que se va a aplicar a cada elemento.A transform function to apply to each element.
Devoluciones
El valor máximo de la secuencia.The maximum value in the sequence.
Excepciones
source
o selector
es null
.source
or selector
is null
.
source
no contiene ningún elemento.source
contains no elements.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) para determinar el valor máximo de una secuencia de valores proyectados.The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the maximum value in a sequence of projected values.
Nota
En este ejemplo de código se usa una sobrecarga de este método sobrecargado diferente de la carga específica que se describe en este tema.This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. Para ampliar el ejemplo a este tema, cambie el cuerpo de la función selector
.To extend the example to this topic, change the body of the selector
function.
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);
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
The maximum pet age plus name length is 14.
*/
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.
Console.WriteLine($"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
Comentarios
El Max<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>) método utiliza la Decimal implementación de IComparable<T> para comparar los valores.The Max<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>) method uses the Decimal implementation of IComparable<T> to compare values.
Este método se puede aplicar a una secuencia de valores arbitrarios si se proporciona una función, selector
, que proyecta los miembros de source
en un tipo numérico, en concreto Decimal .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.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max<TSource>(IEnumerable<TSource>)
Devuelve el valor máximo de una secuencia genérica.Returns the maximum value in a generic sequence.
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource Max(System::Collections::Generic::IEnumerable<TSource> ^ source);
public static TSource Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
public static TSource Max<TSource> (this System.Collections.Generic.IEnumerable<TSource>? source);
static member Max : seq<'Source> -> 'Source
<Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource)) As TSource
Parámetros de tipo
- TSource
Tipo de los elementos de source
.The type of the elements of source
.
Parámetros
- source
- IEnumerable<TSource>
Secuencia de valores cuyo valor máximo se va a determinar.A sequence of values to determine the maximum value of.
Devoluciones
- TSource
El valor máximo de la secuencia.The maximum value in the sequence.
Excepciones
source
es null
.source
is null
.
Ningún objeto de source
implementa la interfaz IComparable o IComparable<T>.No object in source
implements the IComparable or IComparable<T> interface.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max<TSource>(IEnumerable<TSource>) para determinar el valor máximo de una secuencia de IComparable<T> objetos.The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>) to determine the maximum value in a sequence of IComparable<T> objects.
/// <summary>
/// This class implements IComparable to be able to
/// compare one Pet to another Pet.
/// </summary>
class Pet : IComparable<Pet>
{
public string Name { get; set; }
public int Age { get; set; }
/// <summary>
/// Compares this Pet to another Pet by
/// summing each Pet's age and name length.
/// </summary>
/// <param name="other">The Pet to compare this Pet to.</param>
/// <returns>-1 if this Pet is 'less' than the other Pet,
/// 0 if they are equal,
/// or 1 if this Pet is 'greater' than the other Pet.</returns>
int IComparable<Pet>.CompareTo(Pet other)
{
int sumOther = other.Age + other.Name.Length;
int sumThis = this.Age + this.Name.Length;
if (sumOther > sumThis)
return -1;
else if (sumOther == sumThis)
return 0;
else
return 1;
}
}
public static void MaxEx3()
{
Pet[] pets = { new Pet { Name="Barley", Age=8 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=1 } };
Pet max = pets.Max();
Console.WriteLine(
"The 'maximum' animal is {0}.",
max.Name);
}
/*
This code produces the following output:
The 'maximum' animal is Barley.
*/
' This class implements IComparable
' and has a custom 'CompareTo' implementation.
Class Pet
Implements IComparable(Of Pet)
Public Name As String
Public Age As Integer
''' <summary>
''' Compares Pet objects by the sum of their age and name length.
''' </summary>
''' <param name="other">The Pet to compare this Pet to.</param>
''' <returns>-1 if this Pet's sum is 'less' than the other Pet,
''' 0 if they are equal,
''' or 1 if this Pet's sum is 'greater' than the other Pet.</returns>
Function CompareTo(ByVal other As Pet) As Integer _
Implements IComparable(Of Pet).CompareTo
If (other.Age + other.Name.Length > Me.Age + Me.Name.Length) Then
Return -1
ElseIf (other.Age + other.Name.Length = Me.Age + Me.Name.Length) Then
Return 0
Else
Return 1
End If
End Function
End Class
Sub MaxEx3()
' 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}}
' Find the "maximum" pet according to the
' custom CompareTo() implementation.
Dim max As Pet = pets.Max()
' Display the result.
Console.WriteLine($"The 'maximum' animal is {max.Name}")
End Sub
' This code produces the following output:
'
' The 'maximum' animal is Barley
Comentarios
Si TSource
el tipo implementa IComparable<T> , el Max<TSource>(IEnumerable<TSource>) método utiliza esa implementación para comparar los valores.If type TSource
implements IComparable<T>, the Max<TSource>(IEnumerable<TSource>) method uses that implementation to compare values. De lo contrario, si el tipo TSource
implementa IComparable , esa implementación se utiliza para comparar los valores.Otherwise, if type TSource
implements IComparable, that implementation is used to compare values.
Si TSource
es un tipo de referencia y la secuencia de origen está vacía o contiene solo valores que son null
, este método devuelve null
.If TSource
is a reference type and the source sequence is empty or contains only values that are null
, this method returns null
.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.
Consulte también
Se aplica a
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static Nullable<System::Decimal> Max(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<System::Decimal>> ^ selector);
public static decimal? Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,Nullable<decimal>> selector);
static member Max : seq<'Source> * Func<'Source, Nullable<decimal>> -> Nullable<decimal>
<Extension()>
Public Function Max(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Decimal))) As Nullable(Of Decimal)
Parámetros de tipo
- TSource
Tipo de los elementos de source
.The type of the elements of source
.
Parámetros
- source
- IEnumerable<TSource>
Secuencia de valores cuyo valor máximo se va a determinar.A sequence of values to determine the maximum value of.
Función de transformación que se va a aplicar a cada elemento.A transform function to apply to each element.
Devoluciones
Valor de tipo Nullable<Decimal>
en C# o Nullable(Of Decimal)
en Visual Basic que corresponde al valor máximo de la secuencia.The value of type Nullable<Decimal>
in C# or Nullable(Of Decimal)
in Visual Basic that corresponds to the maximum value in the sequence.
Excepciones
source
o selector
es null
.source
or selector
is null
.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) para determinar el valor máximo de una secuencia de valores proyectados.The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) to determine the maximum value in a sequence of projected values.
Nota
En este ejemplo de código se usa una sobrecarga de este método sobrecargado diferente de la carga específica que se describe en este tema.This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. Para ampliar el ejemplo a este tema, cambie el cuerpo de la función selector
.To extend the example to this topic, change the body of the selector
function.
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);
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
The maximum pet age plus name length is 14.
*/
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.
Console.WriteLine($"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
Comentarios
El Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>) método utiliza la Decimal implementación de IComparable<T> para comparar los valores.The Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>) method uses the Decimal implementation of IComparable<T> to compare values.
Este método se puede aplicar a una secuencia de valores arbitrarios si se proporciona una función, selector
, que proyecta los miembros de source
en un tipo numérico, específicamente Nullable<Decimal>
en C# o Nullable(Of Decimal)
en Visual Basic.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 Nullable<Decimal>
in C# or Nullable(Of Decimal)
in Visual Basic.
En Visual Basic sintaxis de expresiones de consulta, una Aggregate Into Max()
cláusula se convierte en una invocación de Max .In Visual Basic query expression syntax, an Aggregate Into Max()
clause translates to an invocation of Max.