@@ -438,5 +438,131 @@ private static IEnumerable<string> ToStringsCore<T>(IEnumerable<T> source)
438
438
foreach ( var obj in source )
439
439
yield return obj ? . ToString ( ) ?? "" ;
440
440
}
441
+
442
+ /// <summary>
443
+ /// Checks, if <paramref name="item"/> is first element of <paramref name="source"/>.
444
+ /// </summary>
445
+ /// <typeparam name="TSource">The type of the elements of source.</typeparam>
446
+ /// <param name="source">An <see cref="IEnumerable{T}"/> to check.</param>
447
+ /// <param name="item">Source item to compare with first element.</param>
448
+ /// <returns>
449
+ /// <c>true</c>, if <paramref name="source"/> has at least one element and first element is equals to
450
+ /// <paramref name="item"/>, otherwise <c>false</c>.
451
+ /// </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
+ }
465
+
466
+ /// <summary>
467
+ /// Checks, if <paramref name="item"/> is first element of <paramref name="source"/>.
468
+ /// </summary>
469
+ /// <typeparam name="TSource">The type of the elements of source.</typeparam>
470
+ /// <param name="source">An <see cref="IEnumerable{T}"/> to check.</param>
471
+ /// <param name="item">Source item to compare with first element.</param>
472
+ /// <param name="comparer">The comparer.</param>
473
+ /// <returns>
474
+ /// <c>true</c>, if <paramref name="source"/> has at least one element and first element is equals to
475
+ /// <paramref name="item"/>, otherwise <c>false</c>.
476
+ /// </returns>
477
+ public static bool IsFirst < TSource > (
478
+ [ NotNull ] this IEnumerable < TSource > source ,
479
+ TSource item ,
480
+ [ CanBeNull ] IEqualityComparer < TSource > comparer )
481
+ {
482
+ Code . NotNull ( source , nameof ( source ) ) ;
483
+
484
+ comparer = comparer ?? EqualityComparer < TSource > . Default ;
485
+
486
+ // Fast path
487
+ // ReSharper disable once CollectionNeverUpdated.Local
488
+ if ( source is IList < TSource > list )
489
+ return comparer . Equals ( item , list [ 0 ] ) ;
490
+
491
+ foreach ( var current in source )
492
+ return comparer . Equals ( item , current ) ;
493
+ return false ;
494
+ }
495
+
496
+ /// <summary>
497
+ /// Checks, if <paramref name="item"/> is last element of <paramref name="source"/>.
498
+ /// </summary>
499
+ /// <typeparam name="TSource">The type of the elements of source.</typeparam>
500
+ /// <param name="source">An <see cref="IEnumerable{T}"/> to check.</param>
501
+ /// <param name="item">Source item to compare with last element.</param>
502
+ /// <returns>
503
+ /// <c>true</c>, if <paramref name="source"/> has at least one element and last element is equals to
504
+ /// <paramref name="item"/>, otherwise <c>false</c>.
505
+ /// </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
+ }
528
+
529
+ /// <summary>
530
+ /// Checks, if <paramref name="item"/> is last element of <paramref name="source"/>.
531
+ /// </summary>
532
+ /// <typeparam name="TSource">The type of the elements of source.</typeparam>
533
+ /// <param name="source">An <see cref="IEnumerable{T}"/> to check.</param>
534
+ /// <param name="item">Source item to compare with last element.</param>
535
+ /// <param name="comparer">The comparer.</param>
536
+ /// <returns>
537
+ /// <c>true</c>, if <paramref name="source"/> has at least one element and last element is equals to
538
+ /// <paramref name="item"/>, otherwise <c>false</c>.
539
+ /// </returns>
540
+ public static bool IsLast < TSource > (
541
+ [ NotNull ] this IEnumerable < TSource > source ,
542
+ TSource item ,
543
+ [ CanBeNull ] IEqualityComparer < TSource > comparer )
544
+ {
545
+ Code . NotNull ( source , nameof ( source ) ) ;
546
+
547
+ comparer = comparer ?? EqualityComparer < TSource > . Default ;
548
+
549
+ // Fast path
550
+ // ReSharper disable once CollectionNeverUpdated.Local
551
+ if ( source is IList < TSource > list )
552
+ return comparer . Equals ( item , list [ list . Count - 1 ] ) ;
553
+
554
+ using ( var en = source . GetEnumerator ( ) )
555
+ if ( en . MoveNext ( ) )
556
+ {
557
+ TSource current ;
558
+ do
559
+ {
560
+ current = en . Current ;
561
+ } while ( en . MoveNext ( ) ) ;
562
+ return comparer . Equals ( item , current ) ;
563
+ }
564
+ else
565
+ return false ;
566
+ }
441
567
}
442
568
}
0 commit comments