mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-11 19:44:55 +00:00
feat: Added MinOrDefault extension method
This commit is contained in:
@@ -1305,6 +1305,37 @@ namespace ICD.Common.Utils.Extensions
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the minimum value from the sequence, otherwise the default value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static T MinOrDefault<T>([NotNull] this IEnumerable<T> extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
Comparer<T> comparer = Comparer<T>.Default;
|
||||
T value = default(T);
|
||||
bool first = true;
|
||||
|
||||
foreach (T x in extends)
|
||||
{
|
||||
if (first)
|
||||
{
|
||||
first = false;
|
||||
value = x;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (comparer.Compare(x, value) < 0)
|
||||
value = x;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the maximum value from the sequence, otherwise the default value.
|
||||
/// </summary>
|
||||
@@ -1319,14 +1350,10 @@ namespace ICD.Common.Utils.Extensions
|
||||
Comparer<T> comparer = Comparer<T>.Default;
|
||||
T value = default(T);
|
||||
|
||||
using (IEnumerator<T> enumerator = extends.GetEnumerator())
|
||||
foreach (T x in extends)
|
||||
{
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
T x = enumerator.Current;
|
||||
if (comparer.Compare(x, value) > 0)
|
||||
value = x;
|
||||
}
|
||||
if (comparer.Compare(x, value) > 0)
|
||||
value = x;
|
||||
}
|
||||
|
||||
return value;
|
||||
|
||||
Reference in New Issue
Block a user