Skip to content

Commit 35f9405

Browse files
committed
Avoid unnecessary boxing
1 parent 69eae7c commit 35f9405

File tree

1 file changed

+8
-35
lines changed

1 file changed

+8
-35
lines changed

Main/src/Collections/Enumerable/EnumerableExtensions.cs

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -449,19 +449,9 @@ private static IEnumerable<string> ToStringsCore<T>(IEnumerable<T> source)
449449
/// <c>true</c>, if <paramref name="source"/> has at least one element and first element is equals to
450450
/// <paramref name="item"/>, otherwise <c>false</c>.
451451
/// </returns>
452-
public static bool IsFirst<TSource>([NotNull] this IEnumerable<TSource> source, TSource item)
453-
{
454-
Code.NotNull(source, nameof(source));
455-
456-
// Fast path
457-
// ReSharper disable once CollectionNeverUpdated.Local
458-
if (source is IList<TSource> list)
459-
return Equals(item, list[0]);
460-
461-
foreach (var current in source)
462-
return Equals(item, current);
463-
return false;
464-
}
452+
[Pure]
453+
public static bool IsFirst<TSource>([NotNull] this IEnumerable<TSource> source, TSource item) =>
454+
source.IsFirst(item, EqualityComparer<TSource>.Default);
465455

466456
/// <summary>
467457
/// Checks, if <paramref name="item"/> is first element of <paramref name="source"/>.
@@ -474,6 +464,7 @@ public static bool IsFirst<TSource>([NotNull] this IEnumerable<TSource> source,
474464
/// <c>true</c>, if <paramref name="source"/> has at least one element and first element is equals to
475465
/// <paramref name="item"/>, otherwise <c>false</c>.
476466
/// </returns>
467+
[Pure]
477468
public static bool IsFirst<TSource>(
478469
[NotNull] this IEnumerable<TSource> source,
479470
TSource item,
@@ -503,28 +494,9 @@ public static bool IsFirst<TSource>(
503494
/// <c>true</c>, if <paramref name="source"/> has at least one element and last element is equals to
504495
/// <paramref name="item"/>, otherwise <c>false</c>.
505496
/// </returns>
506-
public static bool IsLast<TSource>([NotNull] this IEnumerable<TSource> source, TSource item)
507-
{
508-
Code.NotNull(source, nameof(source));
509-
510-
// Fast path
511-
// ReSharper disable once CollectionNeverUpdated.Local
512-
if (source is IList<TSource> list)
513-
return Equals(item, list[list.Count - 1]);
514-
515-
using (var en = source.GetEnumerator())
516-
if (en.MoveNext())
517-
{
518-
TSource current;
519-
do
520-
{
521-
current = en.Current;
522-
} while (en.MoveNext());
523-
return Equals(item, current);
524-
}
525-
else
526-
return false;
527-
}
497+
[Pure]
498+
public static bool IsLast<TSource>([NotNull] this IEnumerable<TSource> source, TSource item) =>
499+
source.IsLast(item, EqualityComparer<TSource>.Default);
528500

529501
/// <summary>
530502
/// Checks, if <paramref name="item"/> is last element of <paramref name="source"/>.
@@ -537,6 +509,7 @@ public static bool IsLast<TSource>([NotNull] this IEnumerable<TSource> source, T
537509
/// <c>true</c>, if <paramref name="source"/> has at least one element and last element is equals to
538510
/// <paramref name="item"/>, otherwise <c>false</c>.
539511
/// </returns>
512+
[Pure]
540513
public static bool IsLast<TSource>(
541514
[NotNull] this IEnumerable<TSource> source,
542515
TSource item,

0 commit comments

Comments
 (0)