Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Quick Search

Advanced Search »
{TOC}
Namespace:System.Linq
Assembly:System.Core.dll
Extends:IEnumerable<T>

Back to Standard Query Operator Index


Edit

Introduction

The Max operator finds the maximum of a sequence of numeric values.

Edit

Method Signatures

// 1 - Return the maximum value
public static Nullable<decimal> Max(
    this IEnumerable<Nullable<decimal>> source)
 
public static long Max(
    this IEnumerable<long> source)
 
public static TSource Max<TSource>(
    this IEnumerable<TSource> source)
 
public static decimal Max(
    this IEnumerable<decimal> source)
 
public static double Max(
    this IEnumerable<double> source)
 
public static int Max(
    this IEnumerable<int> source)
 
public static Nullable<double> Max(
    this IEnumerable<Nullable<double>> source)
 
public static Nullable<int> Max(
    this IEnumerable<Nullable<int>> source)
 
public static Nullable<long> Max(
    this IEnumerable<Nullable<long>> source)
 
public static Nullable<float> Max(
    this IEnumerable<Nullable<float>> source)
 
public static float Max(
    this IEnumerable<float> source)
 
// 2 - Return the maximum value using the select projection.
public static TResult Max<TSource, TResult>(
    this IEnumerable<TSource> source, 
    Func<TSource, TResult> selector)
 
public static Nullable<decimal> Max<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, Nullable<decimal>> selector)
 
public static Nullable<double> Max<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, Nullable<double>> selector)
 
public static decimal Max<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, decimal> selector)
 
public static double Max<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, double> selector)
 
public static int Max<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, int> selector)
 
public static long Max<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, long> selector)
 
public static Nullable<int> Max<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, Nullable<int>> selector)
 
public static Nullable<long> Max<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, Nullable<long>> selector)
 
public static Nullable<float> Max<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, Nullable<float>> selector)
 
public static float Max<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, float> selector)



Edit

Exceptions

Throws an ArgumentNullException if source or selector is null.
Throws an InvalidOperationException if source has no elements and the TSource is not a nullable type.


Edit

Pseudo-code

Overload 1
If source is null, throw an ArgumentNullException.
If source has no elements.
If the TSource is a nullable type.
Make the highest value a nullable TSource instance.
Else.
Throw an InvalidOperationException.
Iterate the source sequence.
If the current element is not null.
If the current value is the first or highest value yet found, remember it.
Return The higest value found.


Overload 2
If source is null, throw an ArgumentNullException.
If source has no elements.
If the TSource is a nullable type.
Make the highest value a nullable TSource instance.
Else.
Throw an InvalidOperationException.
Iterate the source sequence.
Get the current value by projecting the current element using the selector function.
If the current element is not null.
If the current value is the first or highest value yet found, remember it.
Return The highest value found.


Edit

Loop Count

1. All elements from the source sequence are enumerated.


Edit

Code Samples

MaxOperator Unit Tests

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or omissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain unintentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.