. New Idea of net iterator

Source: Internet
Author: User

Under normal circumstances, we are very accustomed to using foreach to iterate over the elements in a container. Although foreach allows us to play fewer letters than for the For loop.

If we have the following requirements: Multiply all elements in a container of an integer by 2 and then save them in a new container, we will usually write down the following code:

1int[] Datas = {1, 3, 2, 5, 7, N, 1, 8,, 8};
2ArrayList src = new ArrayList (datas);
3ArrayList dest = new ArrayList ();
4foreach (Object obj in src)
5{
6 dest. ADD (((int) obj) * 2);
7}
8

For expression, what we really need to pay attention to is "dest." ADD (((int) obj) * 2); " This line of code. In this line of code, the most important is the "((int) obj) * 2" code. And the rest of the code is just to help complete the task above.

Really in development, we may often encounter the above proposition. If you write a foreach every time, and then a few lines of secondary code, it does feel a little cumbersome. So do we have any good methods? It would be nice to encapsulate the foreach.

In the System.Collections.Generic namespace of. NET, we find that the list class has the following methods:

ConvertAll converts the elements in the current list to another type and returns a list containing the converted elements.

Find searches for elements that match the conditions defined by the specified predicate and returns the first matching element in the entire list.

FindAll retrieves all the elements that match the conditions defined by the specified predicate.

FindLast searches for elements that match the conditions defined by the specified predicate and returns the last matching element in the entire list.

FindLastIndex has been overloaded. Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence in the list or part of it.

ForEach performs the specified operation on each element of the List.

All of these methods have a special parameter: agents. These agents are declared in the System namespace.

Below we still follow just the proposition, with ConvertAll to write a piece of code to complete the task.

1int[] Datas = {1, 3, 2, 5, 7, N, 1, 8,, 8};
2list<int> src = new list<int> (datas);
3list<int> dest = src. ConvertAll (delegate (int val)
4{
5 return val * 2;
6});

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.