CSharp examples for System.Collections.Generic:IEnumerable
For each extension that enumerates over an enumerator and attempts to execute the provided action delegate and if the action throws an exception, continues executing.
using System.Collections.Generic; using System;// w ww . j a v a 2 s . c o m public class Main{ /// <summary> /// For each extension that enumerates over an enumerator and attempts to execute the provided /// action delegate and if the action throws an exception, continues executing. /// </summary> /// <typeparam name="T">The type that this extension is applicable for.</typeparam> /// <param name="enumerator">The IEnumerator instace</param> /// <param name="action">The action executed for each item in the enumerator.</param> public static void TryForEach<T>(this IEnumerator<T> enumerator, Action<T> action) { while (enumerator.MoveNext()) { try { action(enumerator.Current); } catch { } } } /// <summary> /// For Each extension that enumerates over a enumerable collection and attempts to execute /// the provided action delegate and it the action throws an exception, continues enumerating. /// </summary> /// <typeparam name="T">The type that this extension is applicable for.</typeparam> /// <param name="collection">The IEnumerable instance that ths extension operates on.</param> /// <param name="action">The action excecuted for each item in the enumerable.</param> public static void TryForEach<T>(this IEnumerable<T> collection, Action<T> action) { foreach (var item in collection) { try { action(item); } catch { } } } }