- remove exception handling to improve performance

ImplementExpressionTrees
Simon G 3 years ago
parent 7556949430
commit 1734d3ee6b
  1. 21
      LightweightIocContainer/EnumerableExtension.cs

@ -13,7 +13,7 @@ internal static class EnumerableExtension
/// <typeparam name="TGiven">The given <see cref="Type"/> to return if the <see cref="IEnumerable{T}"/> contains no elements</typeparam> /// <typeparam name="TGiven">The given <see cref="Type"/> to return if the <see cref="IEnumerable{T}"/> contains no elements</typeparam>
/// <param name="source">The given <see cref="IEnumerable{T}"/></param> /// <param name="source">The given <see cref="IEnumerable{T}"/></param>
/// <returns>The first element of the <see cref="IEnumerable{T}"/>, or a new instance of a given <see cref="Type"/> if the <see cref="IEnumerable{T}"/> contains no elements</returns> /// <returns>The first element of the <see cref="IEnumerable{T}"/>, or a new instance of a given <see cref="Type"/> if the <see cref="IEnumerable{T}"/> contains no elements</returns>
public static TSource FirstOrGiven<TSource, TGiven>(this IEnumerable<TSource> source) where TGiven : TSource, new() => public static TSource FirstOrGiven<TSource, TGiven>(this IReadOnlyCollection<TSource> source) where TGiven : TSource, new() =>
source.TryGetFirst<TSource, TGiven>(null); source.TryGetFirst<TSource, TGiven>(null);
/// <summary> /// <summary>
@ -24,7 +24,7 @@ internal static class EnumerableExtension
/// <param name="source">The given <see cref="IEnumerable{T}"/></param> /// <param name="source">The given <see cref="IEnumerable{T}"/></param>
/// <param name="predicate">A function to test each element for a condition</param> /// <param name="predicate">A function to test each element for a condition</param>
/// <returns>The first element of the <see cref="IEnumerable{T}"/> that satisfies a condition, or a new instance of the given <see cref="Type"/> if no such element is found</returns> /// <returns>The first element of the <see cref="IEnumerable{T}"/> that satisfies a condition, or a new instance of the given <see cref="Type"/> if no such element is found</returns>
public static TSource FirstOrGiven<TSource, TGiven>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) where TGiven : TSource, new() => public static TSource FirstOrGiven<TSource, TGiven>(this IReadOnlyCollection<TSource> source, Func<TSource, bool> predicate) where TGiven : TSource, new() =>
source.TryGetFirst<TSource, TGiven>(predicate); source.TryGetFirst<TSource, TGiven>(predicate);
/// <summary> /// <summary>
@ -35,17 +35,12 @@ internal static class EnumerableExtension
/// <param name="source">The given <see cref="IEnumerable{T}"/></param> /// <param name="source">The given <see cref="IEnumerable{T}"/></param>
/// <param name="predicate">A function to test each element for a condition</param> /// <param name="predicate">A function to test each element for a condition</param>
/// <returns>The first element of the <see cref="IEnumerable{T}"/> or a new instance of the given <see cref="Type"/> when no element is found</returns> /// <returns>The first element of the <see cref="IEnumerable{T}"/> or a new instance of the given <see cref="Type"/> when no element is found</returns>
private static TSource TryGetFirst<TSource, TGiven>(this IEnumerable<TSource> source, Func<TSource, bool>? predicate) where TGiven : TSource, new() private static TSource TryGetFirst<TSource, TGiven>(this IReadOnlyCollection<TSource> source, Func<TSource, bool>? predicate) where TGiven : TSource, new() =>
{ predicate is null ?
try !source.Any() ? new TGiven()
{ : source.First()
return predicate == null ? source.First() : source.First(predicate); : source.Any(predicate) ? source.First(predicate)
} : new TGiven();
catch (Exception)
{
return new TGiven();
}
}
/// <summary> /// <summary>
/// Executes an <see cref="Action{T}"/> for each item in an <see cref="IEnumerable{T}"/> /// Executes an <see cref="Action{T}"/> for each item in an <see cref="IEnumerable{T}"/>

Loading…
Cancel
Save