diff --git a/Benchmarks/Benchmarks.cs b/Benchmarks/Benchmarks.cs index e915aca..d179406 100644 --- a/Benchmarks/Benchmarks.cs +++ b/Benchmarks/Benchmarks.cs @@ -193,17 +193,17 @@ public void Setup() // return intArray.SumF(x => x / 2); //} - [Benchmark] - public double IntReadOnlyArraySumWithSelectLinq() - { - return Array.AsReadOnly(intArray).Sum(x => x / 2); - } + //[Benchmark] + //public double IntReadOnlyArraySumWithSelectLinq() + //{ + // return Array.AsReadOnly(intArray).Sum(x => x / 2); + //} - [Benchmark] - public double IntReadOnlyArraySumWithSelectFast() - { - return Array.AsReadOnly(intArray).SumF(x => x / 2); - } + //[Benchmark] + //public double IntReadOnlyArraySumWithSelectFast() + //{ + // return Array.AsReadOnly(intArray).SumF(x => x / 2); + //} //[Benchmark] //public int IntArraySumFastSIMD() @@ -211,29 +211,29 @@ public double IntReadOnlyArraySumWithSelectFast() // return intArray.SumS(); //} - [Benchmark] - public int? IntNullArraySumLinq() - { - return intNullArray.Sum(); - } + //[Benchmark] + //public int? IntNullArraySumLinq() + //{ + // return intNullArray.Sum(); + //} - [Benchmark] - public int? IntNullArraySumFast() - { - return intNullArray.SumF(); - } + //[Benchmark] + //public int? IntNullArraySumFast() + //{ + // return intNullArray.SumF(); + //} - [Benchmark] - public int IntNullArraySumLinqSelect() - { - return intNullArray.Sum(x => x/2 ?? 0); - } + //[Benchmark] + //public int IntNullArraySumLinqSelect() + //{ + // return intNullArray.Sum(x => x/2 ?? 0); + //} - [Benchmark] - public int IntNullArraySumFastSelect() - { - return intNullArray.SumF(x => x/2 ?? 0); - } + //[Benchmark] + //public int IntNullArraySumFastSelect() + //{ + // return intNullArray.SumF(x => x/2 ?? 0); + //} //[Benchmark] //public float FloatArraySumLinq() @@ -271,29 +271,29 @@ public int IntNullArraySumFastSelect() // return floatArray.SumF(x => x / 2); //} - [Benchmark] - public float? FloatNullArraySumLinq() - { - return floatNullArray.Sum(); - } + //[Benchmark] + //public float? FloatNullArraySumLinq() + //{ + // return floatNullArray.Sum(); + //} - [Benchmark] - public float? FloatNullArraySumFast() - { - return floatNullArray.SumF(); - } + //[Benchmark] + //public float? FloatNullArraySumFast() + //{ + // return floatNullArray.SumF(); + //} - [Benchmark] - public float FloatNullArraySumLinqSelect() - { - return floatNullArray.Sum(x => x / 2 ?? 0); - } + //[Benchmark] + //public float FloatNullArraySumLinqSelect() + //{ + // return floatNullArray.Sum(x => x / 2 ?? 0); + //} - [Benchmark] - public float FloatNullArraySumFastSelect() - { - return floatNullArray.SumF(x => x / 2 ?? 0); - } + //[Benchmark] + //public float FloatNullArraySumFastSelect() + //{ + // return floatNullArray.SumF(x => x / 2 ?? 0); + //} //[Benchmark] @@ -332,41 +332,41 @@ public float FloatNullArraySumFastSelect() // return doubleArray.SumF(x => x / 2); //} - [Benchmark] - public double? DoubleNullArraySumLinq() - { - return doubleNullArray.Sum(); - } + //[Benchmark] + //public double? DoubleNullArraySumLinq() + //{ + // return doubleNullArray.Sum(); + //} - [Benchmark] - public double? DoubleNullArraySumFast() - { - return doubleNullArray.SumF(); - } + //[Benchmark] + //public double? DoubleNullArraySumFast() + //{ + // return doubleNullArray.SumF(); + //} - [Benchmark] - public double? DoubleNullArraySumLinqSelect() - { - return doubleNullArray.Sum(x => x / 2 ?? 0); - } + //[Benchmark] + //public double? DoubleNullArraySumLinqSelect() + //{ + // return doubleNullArray.Sum(x => x / 2 ?? 0); + //} - [Benchmark] - public double? DoubleNullArraySumFastSelect() - { - return doubleNullArray.SumF(x => x / 2 ?? 0); - } + //[Benchmark] + //public double? DoubleNullArraySumFastSelect() + //{ + // return doubleNullArray.SumF(x => x / 2 ?? 0); + //} - [Benchmark] - public double IntArrayAverageLinq() - { - return intArray.Average(); - } + //[Benchmark] + //public double IntArrayAverageLinq() + //{ + // return intArray.Average(); + //} - [Benchmark] - public double IntArrayAverageFast() - { - return intArray.AverageF(); - } + //[Benchmark] + //public double IntArrayAverageFast() + //{ + // return intArray.AverageF(); + //} //[Benchmark] //public double IntArrayAverageFastSIMD() @@ -375,17 +375,17 @@ public double IntArrayAverageFast() //} - [Benchmark] - public double IntListAverageLinq() - { - return intList.Average(); - } + //[Benchmark] + //public double IntListAverageLinq() + //{ + // return intList.Average(); + //} - [Benchmark] - public double IntListAverageFast() - { - return intList.AverageF(); - } + //[Benchmark] + //public double IntListAverageFast() + //{ + // return intList.AverageF(); + //} //[Benchmark] //public double IntListAverageFastSIMD() @@ -393,29 +393,29 @@ public double IntListAverageFast() // return intList.AverageS(); //} - [Benchmark] - public int IntListSumWithSelectLinq() - { - return intList.Sum(x => x / 2); - } + //[Benchmark] + //public int IntListSumWithSelectLinq() + //{ + // return intList.Sum(x => x / 2); + //} - [Benchmark] - public int IntListSumWithSelectFast() - { - return intList.SumF(x => x / 2); - } + //[Benchmark] + //public int IntListSumWithSelectFast() + //{ + // return intList.SumF(x => x / 2); + //} - [Benchmark] - public double IntReadOnlyListSumWithSelectLinq() - { - return intList.AsReadOnly().Sum(x => x / 2); - } + //[Benchmark] + //public double IntReadOnlyListSumWithSelectLinq() + //{ + // return intList.AsReadOnly().Sum(x => x / 2); + //} - [Benchmark] - public double IntReadOnlyListSumWithSelectFast() - { - return intList.AsReadOnly().SumF(x => x / 2); - } + //[Benchmark] + //public double IntReadOnlyListSumWithSelectFast() + //{ + // return intList.AsReadOnly().SumF(x => x / 2); + //} //private static readonly Func mulXInts = (acc, x) => acc += x * x; @@ -581,6 +581,89 @@ public double IntReadOnlyListSumWithSelectFast() // return Array.AsReadOnly(intArray).FirstF(firstInts); //} + private static readonly Func LastInts = (x) => x > 0; + + [Benchmark] + public double IntArrayLastLinq() + { + return intArray.Last(LastInts); + } + + [Benchmark] + public double IntArrayLastFast() + { + return intArray.LastF(LastInts); + } + + [Benchmark] + public double IntArrayLastFast1() + { + Predicate predicate = new Predicate(LastInts); + return Array.Find(intArray, predicate); + } + + + [Benchmark] + public double IntSpanLastForEach() + { + int[] localArray = intArray; + Span asSpan = localArray.AsSpan(); + foreach (int i in asSpan) + { + if (LastInts(i)) + { + return i; + } + } + + return 0; + } + + [Benchmark] + public double IntSpanLastFast() + { + int[] localArray = intArray; + Span asSpan = localArray.AsSpan(); + return asSpan.LastF(LastInts); + } + + [Benchmark] + public double IntListLastLinq() + { + return intList.Last(LastInts); + } + + [Benchmark] + public double IntListLastFast() + { + return intList.LastF(LastInts); + } + + [Benchmark] + public double IntAsListReadOnlyLastLinq() + { + return intList.AsReadOnly().Last(LastInts); + } + + [Benchmark] + public double IntAsListReadOnlyLastFast() + { + return intList.AsReadOnly().LastF(LastInts); + } + + [Benchmark] + public double IntArrayAsReadOnlyLastLinq() + { + return Array.AsReadOnly(intArray).Last(LastInts); + } + + [Benchmark] + public double IntArrayAsReadOnlyLastFast() + { + return Array.AsReadOnly(intArray).LastF(LastInts); + } + + //[Benchmark] //public double IntArrayWhereAggregateLinq() //{ diff --git a/LinqFaster/First.cs b/LinqFaster/First.cs index 9153d37..95383b2 100644 --- a/LinqFaster/First.cs +++ b/LinqFaster/First.cs @@ -9,7 +9,6 @@ namespace JM.LinqFaster { public static partial class LinqFaster { - // -------------------------- Arrays -------------------------------------------- /// @@ -68,6 +67,7 @@ public static T FirstF(this T[] source, Func predicate) /// The array to return the first element of. /// default value if source is empty, otherwise, the first element /// in source. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T FirstOrDefaultF(this T[] source) { if (source == null) diff --git a/LinqFaster/Last.cs b/LinqFaster/Last.cs index d6072e6..abe3632 100644 --- a/LinqFaster/Last.cs +++ b/LinqFaster/Last.cs @@ -1,5 +1,10 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; + +// ReSharper disable ForCanBeConvertedToForeach +// ReSharper disable UnusedMember.Global namespace JM.LinqFaster { @@ -12,11 +17,12 @@ public static partial class LinqFaster /// /// An sequence to return the last element of. /// The value at the last position in the source sequence. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T LastF(this T[] source) { if (source == null) { - throw Error.ArgumentNull("source"); + throw Error.ArgumentNull(nameof(source)); } if (source.Length == 0) { @@ -31,24 +37,27 @@ public static T LastF(this T[] source) /// A sequence to return an element from. /// A function to test each element for a condition. /// - public static T LastF(this T[] source, Predicate predicate) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T LastF(this T[] source, Func predicate) { if (source == null) { - throw Error.ArgumentNull("source"); + throw Error.ArgumentNull(nameof(source)); } if (predicate == null) { - throw Error.ArgumentNull("predicate"); + throw Error.ArgumentNull(nameof(predicate)); } - var lastIndex = Array.FindLastIndex(source, predicate); - - if (lastIndex == -1) - throw Error.NoMatch(); - else - return source[lastIndex]; + for (int i = source.Length - 1; i >= 0; i--) + { + if (predicate(source[i])) + { + return source[i]; + } + } + throw Error.NoMatch(); } /// @@ -57,11 +66,12 @@ public static T LastF(this T[] source, Predicate predicate) /// An sequence to return the last element of. /// default value if the source sequence is empty; otherwise, /// the last element in the sequence + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T LastOrDefaultF(this T[] source) { if (source == null) { - throw Error.ArgumentNull("source"); + throw Error.ArgumentNull(nameof(source)); } if (source.Length == 0) { @@ -78,134 +88,155 @@ public static T LastOrDefaultF(this T[] source) /// default value if the sequence is empty or if no elements pass the test /// in the predicate function; otherwise, the last element that passes the test in the /// predicate function. - public static T LastOrDefaultF(this T[] source, Predicate predicate) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T LastOrDefaultF(this T[] source, Func predicate) { if (source == null) { - throw Error.ArgumentNull("source"); + throw Error.ArgumentNull(nameof(source)); } if (predicate == null) { - throw Error.ArgumentNull("predicate"); + throw Error.ArgumentNull(nameof(predicate)); } - var lastIndex = Array.FindLastIndex(source, predicate); - - if (lastIndex == -1) - return default(T); - else - return source[lastIndex]; + for (int i = source.Length - 1; i >= 0; i--) + { + if (predicate(source[i])) + { + return source[i]; + } + } + return default(T); } - // -------------------------- this Spans -------------------------------------------- + /// - /// Returns the last element of a sequence. + /// Returns the first element of an Array / List / IReadOnlyList. /// - /// An sequence to return the last element of. - /// The value at the last position in the source sequence. - public static T LastF(this Span source) + /// The array to return the first element of. + /// The first element in the specified array. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T LastF(this IReadOnlyList source) { if (source == null) { - throw Error.ArgumentNull("source"); + throw Error.ArgumentNull(nameof(source)); } - if (source.Length == 0) + if (source.Count == 0) { throw Error.NoElements(); } - return source[source.Length - 1]; + return source[source.Count - 1]; } /// - /// Returns the last element of a sequence that satisfies a specified condition. + /// Returns the first element in an array / List / IReadOnlyList that satisfies a specified condition. /// - /// A sequence to return an element from. + /// An IReadOnlyList to return an element from. /// A function to test each element for a condition. - /// - public static T LastF(this Span source, Predicate predicate) + /// The first element that satisfies the condition. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T LastF(this IReadOnlyList source, Func predicate) { - if (source == null) - { - throw Error.ArgumentNull("source"); - } - if (predicate == null) { - throw Error.ArgumentNull("predicate"); + throw Error.ArgumentNull(nameof(predicate)); } - for (int i = source.Length - 1; i >= 0; i--) + switch (source) { - if (predicate(source[i])) return source[i]; + case null: + throw Error.ArgumentNull(nameof(source)); + case T[] sa: + return sa.LastF(predicate); + default: + for (int i = source.Count - 1; i >= 0; i--) + { + if (predicate(source[i])) + { + return source[i]; + } + } + + break; } - - throw Error.NoMatch(); + throw Error.NoMatch(); } /// - /// Returns the last element of a sequence, or a default value if the sequence contains no elements. - /// - /// An sequence to return the last element of. - /// default value if the source sequence is empty; otherwise, - /// the last element in the sequence - public static T LastOrDefaultF(this Span source) + /// Returns the first element of an array, or a default value if the + /// array contains no elements. + /// + /// The array to return the first element of. + /// default value if source is empty, otherwise, the first element + /// in source. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T LastOrDefaultF(this IReadOnlyList source) { if (source == null) { - throw Error.ArgumentNull("source"); + throw Error.ArgumentNull(nameof(source)); } - if (source.Length == 0) + if (source.Count == 0) { return default(T); } - return source[source.Length - 1]; + return source[source.Count - 1]; } /// - /// Returns the last element of a sequence that satisfies a condition or a default value if no such element is found. + /// Returns the first element of the sequence that satisfies a condition or a + /// default value if no such element is found. /// - /// A sequence to return the last element of. + /// An IEnumerable to return an element from. /// A function to test each element for a condition. - /// default value if the sequence is empty or if no elements pass the test - /// in the predicate function; otherwise, the last element that passes the test in the - /// predicate function. - public static T LastOrDefaultF(this Span source, Predicate predicate) + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [Pure] + public static T LastOrDefaultF(this IReadOnlyList source, Func predicate) { - if (source == null) - { - throw Error.ArgumentNull("source"); - } - if (predicate == null) { - throw Error.ArgumentNull("predicate"); + throw Error.ArgumentNull(nameof(predicate)); } - - for (int i = source.Length - 1; i >= 0; i--) + + switch (source) { - if (predicate(source[i])) return source[i]; + case null: + throw Error.ArgumentNull(nameof(source)); + case T[] sa: + return sa.LastOrDefaultF(predicate); + default: + for (int i = source.Count - 1; i >= 0; i--) + { + if (predicate(source[i])) + { + return source[i]; + } + } + break; } - return default(T); - } // -------------------------- Lists -------------------------------------------- /// - /// Returns the last element of a sequence. + /// Returns the first element of a list /// - /// An sequence to return the last element of. - /// The value at the last position in the source sequence. + /// The list to return the first element of. + /// The first element in the specified list. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T LastF(this List source) { if (source == null) { - throw Error.ArgumentNull("source"); + throw Error.ArgumentNull(nameof(source)); } if (source.Count == 0) { @@ -215,42 +246,48 @@ public static T LastF(this List source) } /// - /// Returns the last element of a sequence that satisfies a specified condition. + /// Returns the first element in a list that satisfies a specified condition. /// - /// A sequence to return an element from. + /// An list to return an element from. /// A function to test each element for a condition. - /// + /// The first element in the list that satisfies the condition. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T LastF(this List source, Predicate predicate) { if (source == null) { - throw Error.ArgumentNull("source"); + throw Error.ArgumentNull(nameof(source)); } if (predicate == null) { - throw Error.ArgumentNull("predicate"); + throw Error.ArgumentNull(nameof(predicate)); } - var lastIndex = source.FindLastIndex(predicate); + for (int i = source.Count - 1; i >= 0; i--) + { + if (predicate(source[i])) + { + return source[i]; + } + } - if (lastIndex == -1) - throw Error.NoMatch(); - else - return source[lastIndex]; + throw Error.NoMatch(); } /// - /// Returns the last element of a sequence, or a default value if the sequence contains no elements. - /// - /// An sequence to return the last element of. - /// default value if the source sequence is empty; otherwise, - /// the last element in the sequence + /// Returns the first element of an array, or a default value if the + /// array contains no elements. + /// + /// The array to return the first element of. + /// default value if source is empty, otherwise, the first element + /// in source. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T LastOrDefaultF(this List source) { if (source == null) { - throw Error.ArgumentNull("source"); + throw Error.ArgumentNull(nameof(source)); } if (source.Count == 0) { @@ -260,31 +297,240 @@ public static T LastOrDefaultF(this List source) } /// - /// Returns the last element of a sequence that satisfies a condition or a default value if no such element is found. + /// Returns the first element of the sequence that satisfies a condition or a + /// default value if no such element is found. /// - /// A sequence to return the last element of. + /// An IEnumerable to return an element from. /// A function to test each element for a condition. - /// default value if the sequence is empty or if no elements pass the test - /// in the predicate function; otherwise, the last element that passes the test in the - /// predicate function. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T LastOrDefaultF(this List source, Predicate predicate) { if (source == null) { - throw Error.ArgumentNull("source"); + throw Error.ArgumentNull(nameof(source)); } if (predicate == null) { - throw Error.ArgumentNull("predicate"); + throw Error.ArgumentNull(nameof(predicate)); } - var lastIndex = source.FindLastIndex(predicate); + for (int i = source.Count - 1; i >= 0; i--) + { + if (predicate(source[i])) + { + return source[i]; + } + } + return default(T); + } + + // -------------------------- Span -------------------------------------------- + #region Normal Span + /// + /// Returns the first element of an Span. + /// + /// The Span to return the first element of. + /// The first element in the specified array. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T LastF(this Span source) + { + if (source == null) + { + throw Error.ArgumentNull(nameof(source)); + } + if (source.Length == 0) + { + throw Error.NoElements(); + } + return source[source.Length - 1]; + } + + /// + /// Returns the first element in an Span that satisfies a specified condition. + /// + /// An Span to return an element from. + /// A function to test each element for a condition. + /// The first element that satisfies the condition. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T LastF(this Span source, Func func) + { + if (source == null) + { + throw Error.ArgumentNull(nameof(source)); + } - if (lastIndex == -1) + if (func == null) + { + throw Error.ArgumentNull(nameof(func)); + } + + for (int i = source.Length - 1; i >= 0; i--) + { + if (func(source[i])) + { + return source[i]; + } + } + + throw Error.NoMatch(); + } + + /// + /// Returns the first element of an Span, or a default value if the + /// array contains no elements. + /// + /// The Span to return the first element of. + /// default value if source is empty, otherwise, the first element + /// in source. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T LastOrDefaultF(this Span source) + { + if (source == null) + { + throw Error.ArgumentNull(nameof(source)); + } + if (source.Length == 0) + { return default(T); - else - return source[lastIndex]; + } + return source[source.Length - 1]; + } + + /// + /// Returns the first element of the sequence that satisfies a condition or a + /// default value if no such element is found. + /// + /// An Span to return an element from. + /// A function to test each element for a condition. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T LastOrDefaultF(this Span source, Func func) + { + if (source == null) + { + throw Error.ArgumentNull(nameof(source)); + } + + if (func == null) + { + throw Error.ArgumentNull(nameof(func)); + } + + for (int i = source.Length - 1; i >= 0; i--) + { + if (func(source[i])) + { + return source[i]; + } + } + + return default(T); + } + #endregion Normal Span + + #region ReadOnlySpan + /// + /// Returns the first element of an ReadOnlySpan. + /// + /// The ReadOnlySpan to return the first element of. + /// The first element in the specified array. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T LastF(this ReadOnlySpan source) + { + if (source == null) + { + throw Error.ArgumentNull(nameof(source)); + } + if (source.Length == 0) + { + throw Error.NoElements(); + } + return source[source.Length - 1]; + } + + /// + /// Returns the first element in an ReadOnlySpan that satisfies a specified condition. + /// + /// An ReadOnlySpan to return an element from. + /// A function to test each element for a condition. + /// The first element that satisfies the condition. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T LastF(this ReadOnlySpan source, Func func) + { + if (source == null) + { + throw Error.ArgumentNull(nameof(source)); + } + + if (func == null) + { + throw Error.ArgumentNull(nameof(func)); + } + + for (int i = source.Length - 1; i >= 0; i--) + { + if (func(source[i])) + { + return source[i]; + } + } + + throw Error.NoMatch(); + } + + /// + /// Returns the first element of an ReadOnlySpan, or a default value if the + /// array contains no elements. + /// + /// The ReadOnlySpan to return the first element of. + /// default value if source is empty, otherwise, the first element + /// in source. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T LastOrDefaultF(this ReadOnlySpan source) + { + if (source == null) + { + throw Error.ArgumentNull(nameof(source)); + } + if (source.Length == 0) + { + return default(T); + } + return source[source.Length - 1]; + } + + /// + /// Returns the first element of the sequence that satisfies a condition or a + /// default value if no such element is found. + /// + /// An ReadOnlySpan to return an element from. + /// A function to test each element for a condition. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T LastOrDefaultF(this ReadOnlySpan source, Func func) + { + if (source == null) + { + throw Error.ArgumentNull(nameof(source)); + } + + if (func == null) + { + throw Error.ArgumentNull(nameof(func)); + } + + for (int i = source.Length - 1; i >= 0; i--) + { + if (func(source[i])) + { + return source[i]; + } + } + + return default(T); } + #endregion ReadOnlySpan } } diff --git a/docs/linqfaster.xml b/docs/linqfaster.xml index 1cb17f9..995556c 100644 --- a/docs/linqfaster.xml +++ b/docs/linqfaster.xml @@ -793,7 +793,7 @@ An sequence to return the last element of. The value at the last position in the source sequence. - + Returns the last element of a sequence that satisfies a specified condition. @@ -809,7 +809,7 @@ default value if the source sequence is empty; otherwise, the last element in the sequence - + Returns the last element of a sequence that satisfies a condition or a default value if no such element is found. @@ -819,71 +819,137 @@ in the predicate function; otherwise, the last element that passes the test in the predicate function. - + - Returns the last element of a sequence. + Returns the first element of an Array / List / IReadOnlyList. - An sequence to return the last element of. - The value at the last position in the source sequence. + The array to return the first element of. + The first element in the specified array. - + - Returns the last element of a sequence that satisfies a specified condition. + Returns the first element in an array / List / IReadOnlyList that satisfies a specified condition. - A sequence to return an element from. + An IReadOnlyList to return an element from. A function to test each element for a condition. - + The first element that satisfies the condition. - + - Returns the last element of a sequence, or a default value if the sequence contains no elements. - - An sequence to return the last element of. - default value if the source sequence is empty; otherwise, - the last element in the sequence + Returns the first element of an array, or a default value if the + array contains no elements. + + The array to return the first element of. + default value if source is empty, otherwise, the first element + in source. - + - Returns the last element of a sequence that satisfies a condition or a default value if no such element is found. + Returns the first element of the sequence that satisfies a condition or a + default value if no such element is found. - A sequence to return the last element of. + An IEnumerable to return an element from. A function to test each element for a condition. - default value if the sequence is empty or if no elements pass the test - in the predicate function; otherwise, the last element that passes the test in the - predicate function. + - Returns the last element of a sequence. + Returns the first element of a list - An sequence to return the last element of. - The value at the last position in the source sequence. + The list to return the first element of. + The first element in the specified list. - Returns the last element of a sequence that satisfies a specified condition. + Returns the first element in a list that satisfies a specified condition. - A sequence to return an element from. + An list to return an element from. A function to test each element for a condition. - + The first element in the list that satisfies the condition. - Returns the last element of a sequence, or a default value if the sequence contains no elements. - - An sequence to return the last element of. - default value if the source sequence is empty; otherwise, - the last element in the sequence + Returns the first element of an array, or a default value if the + array contains no elements. + + The array to return the first element of. + default value if source is empty, otherwise, the first element + in source. - Returns the last element of a sequence that satisfies a condition or a default value if no such element is found. + Returns the first element of the sequence that satisfies a condition or a + default value if no such element is found. - A sequence to return the last element of. + An IEnumerable to return an element from. A function to test each element for a condition. - default value if the sequence is empty or if no elements pass the test - in the predicate function; otherwise, the last element that passes the test in the - predicate function. + + + + + Returns the first element of an Span. + + The Span to return the first element of. + The first element in the specified array. + + + + Returns the first element in an Span that satisfies a specified condition. + + An Span to return an element from. + A function to test each element for a condition. + The first element that satisfies the condition. + + + + Returns the first element of an Span, or a default value if the + array contains no elements. + + The Span to return the first element of. + default value if source is empty, otherwise, the first element + in source. + + + + Returns the first element of the sequence that satisfies a condition or a + default value if no such element is found. + + An Span to return an element from. + A function to test each element for a condition. + + + + + Returns the first element of an ReadOnlySpan. + + The ReadOnlySpan to return the first element of. + The first element in the specified array. + + + + Returns the first element in an ReadOnlySpan that satisfies a specified condition. + + An ReadOnlySpan to return an element from. + A function to test each element for a condition. + The first element that satisfies the condition. + + + + Returns the first element of an ReadOnlySpan, or a default value if the + array contains no elements. + + The ReadOnlySpan to return the first element of. + default value if source is empty, otherwise, the first element + in source. + + + + Returns the first element of the sequence that satisfies a condition or a + default value if no such element is found. + + An ReadOnlySpan to return an element from. + A function to test each element for a condition. +