Extended a ForEach Method for IEnumerable, ienumerableforeach

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.