-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIEnumerable.ForEach.cs
274 lines (261 loc) · 12.1 KB
/
IEnumerable.ForEach.cs
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// A part of the C# Language Syntactic Sugar suite.
using System;
using System.Collections.Generic;
namespace CLSS
{
public static partial class IEnumerableForEach
{
/// <summary>
/// Performs the specified action on each element of the
/// <see cref="IEnumerable{T}"/>.
/// </summary>
/// <typeparam name="T">The type of <see cref="IEnumerable{T}"/> to iterate
/// action on.</typeparam>
/// <typeparam name="TElement">The type of the elements of
/// <paramref name="source"/>.</typeparam>
/// <param name="source">The <see cref="IEnumerable{T}"/> to iterate action
/// on.</param>
/// <param name="action">The <see cref="Action{T}"/> delegate to perform on
/// each element of the <see cref="IEnumerable{T}"/>.</param>
/// <returns>The source collection.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or
/// <paramref name="action"/> is null.</exception>
public static T ForEach<T, TElement>(this T source,
Action<TElement> action)
where T : IEnumerable<TElement>
{
if (source == null) throw new ArgumentNullException("source");
if (action == null) throw new ArgumentNullException("action");
foreach (var e in source) action(e);
return source;
}
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"/>
public static IEnumerable<TElement> ForEach<TElement>(
this IEnumerable<TElement> source,
Action<TElement> action)
{
if (source == null) throw new ArgumentNullException("source");
if (action == null) throw new ArgumentNullException("action");
foreach (var e in source) action(e);
return source;
}
/// <summary>
/// Performs the specified function on each element of the
/// <see cref="IEnumerable{T}"/>.
/// </summary>
/// <typeparam name="T">
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/typeparam[@name='T']"/></typeparam>
/// <typeparam name="TElement">
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/typeparam[@name='TElement']"/></typeparam>
/// <typeparam name="TResult">The return type of the <paramref name="func"/>
/// delegate.</typeparam>
/// <param name="source">
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/param[@name='source']"/></param>
/// <param name="func">The <see cref="Func{T, TResult}"/> delegate to
/// perform on each element of the <see cref="IEnumerable{T}"/>.</param>
/// <returns><inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/returns"/></returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or
/// <paramref name="func"/> is null.</exception>
public static T ForEach<T, TElement, TResult>(this T source,
Func<TElement, TResult> func)
where T : IEnumerable<TElement>
{
if (source == null) throw new ArgumentNullException("source");
if (func == null) throw new ArgumentNullException("func");
foreach (var e in source) func(e);
return source;
}
/// <inheritdoc cref="ForEach{T, TElement, TResult}(T, Func{TElement, TResult})"/>
public static IEnumerable<TElement> ForEach<TElement, TResult>(
this IEnumerable<TElement> source,
Func<TElement, TResult> func)
{
if (source == null) throw new ArgumentNullException("source");
if (func == null) throw new ArgumentNullException("func");
foreach (var e in source) func(e);
return source;
}
/// <summary>
/// Performs the specified action on each element of the
/// <see cref="IEnumerable{T}"/>, passing through the element's index.
/// </summary>
/// <typeparam name="T">
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/typeparam[@name='T']"/></typeparam>
/// <typeparam name="TElement">
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/typeparam[@name='TElement']"/></typeparam>
/// <param name="source">
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/param[@name='source']"/></param>
/// <param name="action">The <see cref="Action{T1, T2}"/> delegate to
/// perform on each element of the <see cref="IEnumerable{T}"/>, with its
/// second argument being the element's index.</param>
/// <returns><inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/returns"/></returns>
/// <exception cref="ArgumentNullException">
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/exception[@cref='ArgumentNullException']"/></exception>
public static T ForEach<T, TElement>(this T source,
Action<TElement, int> action)
where T : IEnumerable<TElement>
{
if (source == null) throw new ArgumentNullException("source");
if (action == null) throw new ArgumentNullException("action");
int index = 0;
foreach (var e in source) { action(e, index); ++index; }
return source;
}
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement, int})"/>
public static IEnumerable<TElement> ForEach<TElement>(
this IEnumerable<TElement> source,
Action<TElement, int> action)
{
if (source == null) throw new ArgumentNullException("source");
if (action == null) throw new ArgumentNullException("action");
int index = 0;
foreach (var e in source) { action(e, index); ++index; }
return source;
}
/// <summary>
/// Performs the specified function on each element of the
/// <see cref="IEnumerable{T}"/>, passing through the element's index.
/// </summary>
/// <typeparam name="T">
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/typeparam[@name='T']"/></typeparam>
/// <typeparam name="TElement">
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/typeparam[@name='TElement']"/></typeparam>
/// <typeparam name="TResult">
/// <inheritdoc cref="ForEach{T, TElement, TResult}(T, Func{TElement, TResult})"
/// path="/typeparam[@name='TResult']"/></typeparam>
/// <param name="source">
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/param[@name='source']"/></param>
/// <param name="func">The <see cref="Func{T1, T2, TResult}"/> delegate to
/// perform on each element of the <see cref="IEnumerable{T}"/>, with its
/// second argument being the element's index.</param>
/// <returns><inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/returns"/></returns>
/// <exception cref="ArgumentNullException">
/// <inheritdoc cref="ForEach{T, TElement, TResult}(T, Func{TElement, TResult})"
/// path="/exception[@cref='ArgumentNullException']"/></exception>
public static T ForEach<T, TElement, TResult>(this T source,
Func<TElement, int, TResult> func)
where T : IEnumerable<TElement>
{
if (source == null) throw new ArgumentNullException("source");
if (func == null) throw new ArgumentNullException("func");
int index = 0;
foreach (var e in source) { func(e, index); ++index; }
return source;
}
/// <inheritdoc cref="ForEach{T, TElement, TResult}(T, Func{TElement, int, TResult})"/>
public static IEnumerable<TElement> ForEach<TElement, TResult>(
this IEnumerable<TElement> source,
Func<TElement, int, TResult> func)
{
if (source == null) throw new ArgumentNullException("source");
if (func == null) throw new ArgumentNullException("func");
int index = 0;
foreach (var e in source) { func(e, index); ++index; }
return source;
}
/// <summary>
/// Performs the specified action on each element of the
/// <see cref="IEnumerable{T}"/>, passing through the element's index and
/// the collection being iterated on.
/// </summary>
/// <typeparam name="T">
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/typeparam[@name='T']"/></typeparam>
/// <typeparam name="TElement">
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/typeparam[@name='TElement']"/></typeparam>
/// <param name="source">
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/param[@name='source']"/></param>
/// <param name="action">The <see cref="Action{T1, T2, T3}"/> delegate to
/// perform on each element of the <see cref="IEnumerable{T}"/>, with its
/// second argument being the element's index and the third argument being
/// <paramref name="source"/>.</param>
/// <returns><inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/returns"/></returns>
/// <exception cref="ArgumentNullException">
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/exception[@cref='ArgumentNullException']"/></exception>
public static T ForEach<T, TElement>(this T source,
Action<TElement, int, T> action)
where T : IEnumerable<TElement>
{
if (source == null) throw new ArgumentNullException("source");
if (action == null) throw new ArgumentNullException("action");
int index = 0;
foreach (var e in source) { action(e, index, source); ++index; }
return source;
}
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement, int, T})"/>
public static IEnumerable<TElement> ForEach<TElement>(
this IEnumerable<TElement> source,
Action<TElement, int, IEnumerable<TElement>> action)
{
if (source == null) throw new ArgumentNullException("source");
if (action == null) throw new ArgumentNullException("action");
int index = 0;
foreach (var e in source) { action(e, index, source); ++index; }
return source;
}
/// <summary>
/// Performs the specified function on each element of the
/// <see cref="IEnumerable{T}"/>, passing through the element's index and
/// the collection being iterated on.
/// </summary>
/// <typeparam name="T">
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/typeparam[@name='T']"/></typeparam>
/// <typeparam name="TElement">
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/typeparam[@name='TElement']"/></typeparam>
/// <typeparam name="TResult">
/// <inheritdoc cref="ForEach{T, TElement, TResult}(T, Func{TElement, TResult})"
/// path="/typeparam[@name='TResult']"/></typeparam>
/// <param name="source">
/// <inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/param[@name='source']"/></param>
/// <param name="func">The <see cref="Func{T1, T2, T3, T4}"/> delegate to
/// perform on each element of the <see cref="IEnumerable{T}"/>, with its
/// second argument being the element's index and the third argument being
/// <paramref name="source"/>.</param>
/// <returns><inheritdoc cref="ForEach{T, TElement}(T, Action{TElement})"
/// path="/returns"/></returns>
/// <exception cref="ArgumentNullException">
/// <inheritdoc cref="ForEach{T, TElement, TResult}(T, Func{TElement, TResult})"
/// path="/exception[@cref='ArgumentNullException']"/></exception>
public static T ForEach<T, TElement, TResult>(this T source,
Func<TElement, int, T, TResult> func)
where T : IEnumerable<TElement>
{
if (source == null) throw new ArgumentNullException("source");
if (func == null) throw new ArgumentNullException("func");
int index = 0;
foreach (var e in source) { func(e, index, source); ++index; }
return source;
}
/// <inheritdoc cref="ForEach{T, TElement, TResult}(T, Func{TElement, int, T, TResult})"/>
public static IEnumerable<TElement> ForEach<T, TElement, TResult>(
this IEnumerable<TElement> source,
Func<TElement, int, IEnumerable<TElement>, TResult> func)
{
if (source == null) throw new ArgumentNullException("source");
if (func == null) throw new ArgumentNullException("func");
int index = 0;
foreach (var e in source) { func(e, index, source); ++index; }
return source;
}
}
}