Skip to content

An extension method to provide MinBy and MaxBy as found in .NET 6 to projects running on an earlier runtime version. A part of the C# Language Syntactic Sugar suite.

License

Notifications You must be signed in to change notification settings

tonygiang/CLSS.ExtensionMethods.IEnumerable.MinMaxBy

Repository files navigation

CLSS.ExtensionMethods.IEnumerable.MinMaxBy

Problem

MinBy & MaxBy are new LINQ extension methods first introduced in .NET 6. Visit their reference documentation links to learn what they do. These methods have the following drawbacks:

  • They are not available in .NET runtime environments earlier than .NET 6, which are most of the .NET runtime environments.
  • They do not statically ensure that key types implement the IComparable or IComparable<T> interface. It is possible to pass in a selector of invalid type and get no compile-time error, but you will get a runtime exception.
  • They don't check for null key selector.
  • They don't check for empty source collection.

Solution

This package backports MinBy and MaxBy for .NET versions before .NET 6 that are also compatible with .NET Standard 1.0. Learn more about compatible runtime environments here. In the process, they also rectified the above drawbacks.

Statically-checked type:

using CLSS;

public struct CustomType
{
  // System.Uri does not implement IComparable or IComparable<T>
  public System.Uri Schema;
}

var collection = new CustomType[]
{
  new CustomType { Schema = new System.Uri("uri1") },
  new CustomType { Schema = new System.Uri("uri2") },
  new CustomType { Schema = new System.Uri("uri3") }
};
// Compilation error with CLSS version of MaxBy
var max = collection.MaxBy(e => e.Schema);

Null-checked key selector:

using CLSS;

var numbers = new int[] { 6, 3, 17 };
var max = numbers.MaxBy(null); // Throws ArgumentNullException

Empty-checked source collection:

using CLSS;

var timestampts = new System.DateTime[0];
var max = numbers.MaxBy(t => t.Year); // Throws InvalidOperationException

Although this package's MinBy and MaxBy statically ensure that the key selector returns an IComparable or IComparable<T>, passing in an optional custom IComparer<T> will loosen this requirement and allow key selectors that return any type.

This package is a part of the C# Language Syntactic Sugar suite.

About

An extension method to provide MinBy and MaxBy as found in .NET 6 to projects running on an earlier runtime version. A part of the C# Language Syntactic Sugar suite.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages