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?