comparison Chronosv2/source/Extensions/EnumerableExtensions.cs @ 10:443821e55f06

Initial cleaned up add from Codeplex files
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:25:44 +0700
parents
children
comparison
equal deleted inserted replaced
9:904a9faadf8b 10:443821e55f06
1 using System;
2 using System.Collections.Generic;
3
4 namespace Chronos.Extensions
5 {
6 /// <summary>
7 /// Extension methods for all kinds of (typed) enumerable data (Array, List, ...)
8 /// </summary>
9 public static class EnumerableExtensions
10 {
11 #region · Extensions ·
12
13 /// <summary>
14 /// Converts all items of a list and returns them as enumerable.
15 /// </summary>
16 /// <typeparam name="TSource">The source data type</typeparam>
17 /// <typeparam name="TTarget">The target data type</typeparam>
18 /// <param name="source">The source data.</param>
19 /// <returns>The converted data</returns>
20 /// <example>
21 ///
22 /// var values = new[] { "1", "2", "3" };
23 /// values.ConvertList&lt;string, int&gt;().ForEach(Console.WriteLine);
24 ///
25 /// </example>
26 public static IEnumerable<TTarget> ConvertList<TSource, TTarget>(this IEnumerable<TSource> source)
27 {
28 foreach (var value in source)
29 {
30 yield return value.ConvertTo<TTarget>();
31 }
32 }
33
34 /// <summary>
35 /// Performs an action for each item in the enumerable
36 /// </summary>
37 /// <typeparam name="T">The enumerable data type</typeparam>
38 /// <param name="values">The data values.</param>
39 /// <param name="action">The action to be performed.</param>
40 /// <example>
41 ///
42 /// var values = new[] { "1", "2", "3" };
43 /// values.ConvertList&lt;string, int&gt;().ForEach(Console.WriteLine);
44 ///
45 /// </example>
46 /// <remarks>This method was intended to return the passed values to provide method chaining. Howver due to defered execution the compiler would actually never run the entire code at all.</remarks>
47 public static void ForEach<T>(this IEnumerable<T> values, Action<T> action)
48 {
49 foreach (var value in values)
50 {
51 action(value);
52 }
53 }
54
55 #endregion
56 }
57 }