Extended a ForEach Method for IEnumerable, ienumerableforeach
IEnumerable does not have a ForEach method. We can use C # To write an extension method:
Source Code:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Insus. NET. extendMethods {public static class Enumerables {public static void ForEach <T> (this IEnumerable <T> source, Action <T> action) {foreach (T item in source) {action (item );}}}}View Code
In the following example, Insus. NET lists an IEnumerable dataset and uses this method to obtain the elements:
Source Code:
Public IEnumerable <IDictionary <string, object> Links () {var dict = new Dictionary <string, object> (); dict ["Index"] = "new Product Introduction "; dict ["Manufacturing"] = "Manufacturing capability/process"; dict ["DieCasting"] = "Die Casting"; dict ["Machining"] = "processing "; dict ["AssemblyFinishing"] = "assembly & Finished Products"; dict ["TestReliability"] = "test and reliability"; dict ["KeyCustomer"] = "key customers "; yield return dict ;}View Code
ForEach method application:
Source Code:
Obj. links (). forEach (delegate (IDictionary <string, object> dict) {foreach (KeyValuePair <string, object> kvp in dict) {// kvp. key // kvp. value }});View Code