|
|
@ -731,13 +731,27 @@ namespace Apewer |
|
|
|
if (selector == null) throw new ArgumentNullException(nameof(selector)); |
|
|
|
if (collection == null) return new TOut[0]; |
|
|
|
|
|
|
|
return Map(collection, (item, index) => selector.Invoke(item)); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>遍历集合,转换元素,生成新数组。</summary>
|
|
|
|
/// <typeparam name="TIn">输入的元素类型。</typeparam>
|
|
|
|
/// <typeparam name="TOut">输出的元素类型。</typeparam>
|
|
|
|
/// <param name="collection">要遍历的集合。</param>
|
|
|
|
/// <param name="selector">转换程序。</param>
|
|
|
|
/// <returns>新数组的元素类型。</returns>
|
|
|
|
public static TOut[] Map<TIn, TOut>(this IEnumerable<TIn> collection, Func<TIn, int, TOut> selector) |
|
|
|
{ |
|
|
|
if (selector == null) throw new ArgumentNullException(nameof(selector)); |
|
|
|
if (collection == null) return new TOut[0]; |
|
|
|
|
|
|
|
if (collection is TIn[] array) |
|
|
|
{ |
|
|
|
var count = array.Length; |
|
|
|
var result = new TOut[count]; |
|
|
|
for (var i = 0; i < count; i++) |
|
|
|
{ |
|
|
|
result[i] = selector.Invoke(array[i]); |
|
|
|
result[i] = selector.Invoke(array[i], i); |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
@ -753,7 +767,7 @@ namespace Apewer |
|
|
|
capacity += 1024; |
|
|
|
list.Capacity += capacity; |
|
|
|
} |
|
|
|
list.Add(selector.Invoke(item)); |
|
|
|
list.Add(selector.Invoke(item, count)); |
|
|
|
count += 1; |
|
|
|
} |
|
|
|
return list.ToArray(); |
|
|
|