-
Notifications
You must be signed in to change notification settings - Fork 4
/
EnumerableEx.First.cs
72 lines (65 loc) · 3.04 KB
/
EnumerableEx.First.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace Gapotchenko.FX.Linq
{
partial class EnumerableEx
{
/// <summary>
/// Returns the first element of a sequence, or the specified default value if the sequence contains no elements.
/// </summary>
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}"/> to return the first element of.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>
/// <paramref name="defaultValue"/> if source is empty;
/// otherwise, the first element in source.
/// </returns>
#if TFF_ENUMERABLE_FIRSTORDEFAULT_VALUE
[EditorBrowsable(EditorBrowsableState.Never)]
public static TSource FirstOrDefault<TSource>(IEnumerable<TSource> source, TSource defaultValue) => Enumerable.FirstOrDefault(source, defaultValue);
#else
public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source, TSource defaultValue)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
using var enumerator = source.GetEnumerator();
if (!enumerator.MoveNext())
{
// The sequence is empty.
return defaultValue;
}
return enumerator.Current;
}
#endif
/// <summary>
/// Returns the first element of the sequence that satisfies a condition or the specified default value if no such element is found.
/// </summary>
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}"/> to return the an element from.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>
/// <paramref name="defaultValue"/> if source is empty or if no element passes the test specified by predicate;
/// otherwise, the first element in source that passes the test specified by predicate.
/// </returns>
#if TFF_ENUMERABLE_FIRSTORDEFAULT_VALUE
[EditorBrowsable(EditorBrowsableState.Never)]
public static TSource FirstOrDefault<TSource>(IEnumerable<TSource> source, Func<TSource, bool> predicate, TSource defaultValue) =>
Enumerable.FirstOrDefault(source, predicate, defaultValue);
#else
public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, TSource defaultValue)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (predicate == null)
throw new ArgumentNullException(nameof(predicate));
foreach (var i in source)
if (predicate(i))
return i;
return defaultValue;
}
#endif
}
}