Several commonly used foreach extensions, and a first method is provided.

Source: Internet
Author: User

Nowadays, it seems that many people do not like to write for, foreach, do, and while loops. tolist. foreach is used. In fact, this is also true for me, but in. net, only ilist <t> has this method. In many cases, I need to use the same method in icollection <t>, ienumerable <t>, or even ienumerable. Besides, the built-in method is also simpler. There is only one traversal function, which does not support interruptions and no indexes ...... Is it inconvenient?

 

Very simple. If ms is not provided, let's do it on our own. ViewCodeRight.

 

/// <Summary> /// basically, it is consistent with the foreach method of list <t>. /// </Summary> Public static void each <t> (this ienumerable <t> Col, Action <t> handler) {foreach (VAR item in col) handler (item) ;}/// <summary> /// the Traversal method with an index. /// </Summary> Public static void each <t> (this ienumerable <t> Col, Action <t, int> handler) {int Index = 0; foreach (VAR item in col) handler (item, index ++) ;}/// <summary> /// the Traversal method that can be interrupted on the half-way. /// </Summary> Public static void each <t> (this ienumerable <t> Col, func <t, bool> handler) {foreach (VAR item in col) if (! Handler (item) break;} /// <summary> // you can use an indexed Traversal method for a half-way segment. /// </Summary> Public static void each <t> (this ienumerable <t> Col, func <t, Int, bool> handler) {int Index = 0; foreach (VAR item in col) if (! Handler (item, index ++) break;} # The following region is a non-generic implementation of public static void each for ienumerable <t> (this ienumerable Col, action <Object> handler) {foreach (VAR item in col) handler (item);} public static void each <t> (this ienumerable Col, Action <object, int> handler) {int Index = 0; foreach (VAR item in col) handler (item, index ++);} public static void each <t> (this ienumerable Col, func <object, bool> Han Dler) {foreach (VAR item in col) if (! Handler (item) break;} public static void each <t> (this ienumerable Col, func <object, Int, bool> handler) {int Index = 0; foreach (VAR item in col) if (! Handler (item, index ++) break;} # endregion

Another one is that the first method in LINQ is very useful, but for dynamically generated classes, there is no way to get ilist <t> before compilation, so there is no way to use the first () method, it is inconvenient for testing and debugging. We also need to expand one.

 

 

 
/// <Summary> /// return the first element to a non-strong ienumerable. /// </Summary> Public static object first (this ienumerable col) {foreach (VAR item in col) return item; throw new indexoutofrangeexception ();} /// <summary> /// return a strong element in the header to a non-strong ienumerable /// </Summary> Public static object first <t> (this ienumerable col) {return (t) Col. first ();}

 

 

Of course, this is very simple, because we have nothing to do with the reload of another expression, but it is enough for testing. It's not easy to implement that overload. Let's think about it!

 

-------------------------

 

Summary: although the method is simple, it is very easy to code, and it improves a lot of work efficiency. Why?

 

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.