Repository navigation
Expand file tree
/
Copy pathForEachExtensions.cs
More file actions
49 lines (45 loc) · 1.82 KB
/
Copy pathForEachExtensions.cs
File metadata and controls
49 lines (45 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using ByteDecoder.Common.GuardClauses;
namespace ByteDecoder.RoyalLibrary
{
/// <summary>
///
/// </summary>
public static class ForEachExtensions
{
/// <summary>
/// Applies the action on each element in the current collection. Do not return a new
/// collection and do not mutate the current collection
/// </summary>
/// <typeparam name="T">Element collection type</typeparam>
/// <param name="source">Current collection</param>
/// <param name="action">Applied action on to transform each collection element</param>
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
Guard.Break.IfArgumentIsNull(source, nameof(source));
Guard.Break.IfArgumentIsNull(action, nameof(action));
foreach (var element in source)
{
action(element);
}
}
/// <summary>
/// Applies the action on each element in the current collection and passes back on each iteration the
/// current index. Do not return a new collection and do not mutate the current collection
/// </summary>
/// <typeparam name="T">Element collection type</typeparam>
/// <param name="source">Current collection</param>
/// <param name="action">Applied action on to transform each collection element</param>
public static void ForEach<T>(this IEnumerable<T> source, Action<T, int> action)
{
Guard.Break.IfArgumentIsNull(source, nameof(source));
Guard.Break.IfArgumentIsNull(action, nameof(action));
var index = 0;
foreach (var element in source)
{
action(element, index++);
}
}
}
}