Summarize IEnumerable examples

This article deals around " 10 small example of IEnumerable try " and " the other six small example to try IEnumerable of examples" given to sum up for  IEnumerable some of the use of the interface, and I hope readers can derive some inspiration.

Type of iterative framework

For a realization of  IEnumerable the interface type, the development of the most commonly used, the type of the object is to put the  foreach other loop iteration keyword, which is treated through the elements.

This traversal is usually divided into two purposes: to traverse and search.

IEnumerable And its generic version  IEnumerable<T> defines "may iteration" of a type. This is easy to understand the system a lot of collection types implement this interface.

Thus these types can be set using  foreach iterative traversal. However, in an iterative manner, and each set of results is not exactly the same type, depending on the characteristics of the collection itself. E.g:

  • List<>, Stack<> And  Queue<> the sequence of iterations is not the same, because the data structure itself are different requirements
  • ConcurrentDictionary<,> And  Dictionary<,> thread safety when iteration is different, because for the thread-safe design is different
  • BlockingCollection.GetConsumingEnumerable The method will produce a return of consumer objects do not block,

So, even if all thrown into  foreach, but the effect is not exactly the same. Using these, the need for these types of readers themselves need to enhance mutual understanding.

The reader is advised in the use of the framework to achieve the  IEnumerable type, be sure to pay attention to the details of iteration, we can understand its particularity through the document on MSDN.

Linq

Linq is a topic that small is not small, but here say Linq To Object part of it.

Through some Linq extension methods provided, a convenient control for  IEnumerable iteratively object. By application of these methods, it can be avoided and complicated conditions in many cases loop nest.

Meanwhile, Linq in abstract Func and Action, also requires developers to pay attention to classify and organize itself for the iterative process of writing the usual. Where(IsLeapYear) Than would  Where(x=>(x % 4 == 0 && x % 100 != 0) || x % 400 == 0) be easier to read.

Design complex data structures and Iteration

In addition to basic data structure, the development process may need to customize some collection type. These collection types need to implement an iterative process. For example: binary tree and its traversal, the list page, and so on.

These iterative data structures are usually required to support a particular algorithm.

In the " Also try IEnumerable 6 small example of " the tree will be data on a few examples of this class.

Local function

In C # 7.0 introduces a local function after,  IEnumerable with local function, iterative process quickly implement custom operation will follow the strange appearance.

It may be employed to achieve some of the ways previously not easily achieved by such an operation of some operation in a function:

  • The multi-leveled cycle
  • The multi-stage determination condition determination cycle becomes
  • No need to create a new class will be able to quickly generate a special context requires iterative algorithm

This is relevant examples "in 10 small example of IEnumerable try " in more.

According to month boss saying: "Business is not because of the complexity of the system and reduce design changes, it's just moved from one place to another place." We know, in fact, such an approach makes no judgment has always had and cycling is reduced. Just change the grammatical structure.

Readers can this operation as a "syntactic sugar" for use. If you are in a team project, you need to respect the views of the team members together, because this operation not everyone is willing to accept.

Of course, this practice will produce benefits in some places. For example, the local function, IEnumerable combined and Task T10 test the network connection. Such an approach reduces the need to create a written tradition  List or  Array overhead.

In short, such an approach provides a new way of thinking. Is it necessary to use will depend on the degree of acceptance of the readers of the team.

Asynchronous iterator

In C # 8 and arrival .netcore 3.0 version, we welcome to the  IAsyncEnumerable functional interface to implement asynchronous iterator.

IEnumerable Iterators synchronization method, IAsyncEnumerable can be seen as its asynchronous version. With this interface, in an iterative process also can take advantage of async / await bring programming pleasure.

Examples of this series did not add this part, but the main idea is the same.

Her appearance will only make it easier for developers of several major application scenarios summarized above.

Detailed examples, can see the related articles to understand .

to sum up

This will be the end of this series, we hope readers understand more than a few summarized above in practice usage scenarios.

Examples in this series have all been using  dotnetfiddle.net  been rewritten, readers can directly run these examples on this blog page.

If you can not properly display example, readers can also download the code samples related through this warehouse .

Guess you like

Origin www.cnblogs.com/newbe36524/p/11879133.html