- 
      
- 
        Save elvisgs/956482 to your computer and use it in GitHub Desktop. 
    MaxOrDefault sem catch
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public static TSource MaxOrDefault<TSource>(this IEnumerable<TSource> source) | |
| where TSource : IComparable | |
| { | |
| return source.MaxOrDefault(default(TSource)); | |
| } | |
| public static TSource MaxOrDefault<TSource>(this IEnumerable<TSource> source, TSource defaultValue) | |
| where TSource : IComparable | |
| { | |
| return source.Aggregate(defaultValue, (a, b) => a != null && a.CompareTo(b) > 0 ? a : b); | |
| } | |
| public static TSource MaxOrDefault<TSource>(this IEnumerable<TSource?> source) | |
| where TSource : struct, IComparable | |
| { | |
| return source.MaxOrDefault(default(TSource)); | |
| } | |
| public static TSource MaxOrDefault<TSource>(this IEnumerable<TSource?> source, TSource defaultValue) | |
| where TSource : struct, IComparable | |
| { | |
| return source.Where(x => x != null).Select(x => x ?? default(TSource)).MaxOrDefault(defaultValue); | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment