. Net2.0 set operation-What I hope?

Source: Internet
Author: User

In my previous article, I mentioned not to scatter the traversal set operations in the code, but to put the traversal set operations into the set itself.
At that time, I wanted to create a Typed Collection in. Net1.0. In this way, the traversal set operation can be put into the set itself.
However, the generic mechanism is provided in. Net2.0. I can't always restore the style to hide the traversal operation of the Set (do the Typed Collection by myself.
Fortunately, we found that. Net2.0 has come up with this for me. You will find that the ForEach (Action <T>) method is provided in. Net2.0 List <T>.

Class Program
{
Static void Main (string [] args)
{
List <string> strs = new List <string> ();
Strs. Add ("hello ");
Strs. Add ("world ");

Strs. ForEach (Console. WriteLine)
}
}

How is it? You cannot see operations like foreach (string str in strs.
Why? For specific reasons and implementation methods under. Net1.0, please refer to here (the article is a bit long, be patient, and I should be rewarded after reading it)

However, List <T> is not perfect for me. It cannot do something for all the members meeting specific conditions in the set.
That is, a method similar to the following is provided:

 

ForSpecification (Predicate <T>, Action <T> );

Wouldn't it be perfect? Since ForEach is done, why does MS not make ForSpecification?

Now I want to implement this function. I can only use the Find (Predicate <T>) method to Find the matching elements in the set, and then traverse it (really ugly)

Just like implementing a set operation helper class under. Net1.0. But with the support of generics, the implementation method is certainly much simpler than before. However, the purpose and philosophy are the same.

Class Algorithm
{

Public static void ForSpecification <T> (IEnumerable <T> collection, Action <T> action, Predicate <T> filter)
{
Foreach (T obj in collection)
{
If (filter (obj ))
Action (obj );
}
}
}

Class Program
{
Static void Main (string [] args)
{
IList <string> strs = new List <string> ();
Strs. Add ("hello ");
Strs. Add ("world ");
Strs. Add ("hide ");

Algorithm. ForSpecification <string> (
Strs,
Console. WriteLine,
Delegate (string str)
{
// Return true; Hide will be output
Return str. Contains ("o"); // if string contains 'O' then output
});
}
}

OK? This helper class can be used to complete corresponding operations even if the ForEach and Find methods are not supported.
In fact, you can also add some methods in the helper class, such as adding elements in the set and returning results, so that you can implement them by yourself.

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.