C # extensions: enumerable. Cast Application

Source: Internet
Author: User
Enumerable. Cast Used to convert ienumerable to a generic version of ienumerable . After conversion, you can enjoy other enumerable methods (such as where and select), which greatly facilitates our coding.

However, only one example of converting arraylist is provided in msdn. Many people have seen that list is currently used. Who else will use arraylist, cast? It is of little use unless it processes some of the Code left behind.

Actually, cast It is not that simple. It can be used in many places.

Let's take a look at the example in msdn:

1 system. Collections. arraylist fruits = new system. Collections. arraylist ();
2 fruits. Add ("apple ");
3 fruits. Add ("Mango ");
4
5 ienumerable Query = fruits. Cast ();
6 foreach (string fruit in query) console. writeline (fruit );

This example is simple and easy to understand.

The other collection classes in. Net 1.x can also be used as above, such as array and non-generic list...

Interrupt. Is there a list of non-generic versions? I have never used. Net 1.x, but it is not clear, but the form control contains a list control (ASP. NET) and a listview control (winform ).

Take listview as an example. The listview control can contain many items or a set. Let's take a look at its items attributes!

1 public class listview: Control
2 {
3
4 Public listview. listviewitemcollection items {Get ;}
5
6 public class listviewitemcollection: ilist, icollection, ienumerable {}
7
8}

The items type of listview is listview. listviewitemcollection, which implements ienumerable.

Listview. items is a non-generic set, so cast can be applied. .

The following code assumes that the ListBox data is bound to a set of employees:

1 int COUNT = ListBox. Items. Cast (). Count ();
2 bool B = ListBox. Items. Cast (). Any (E => E. firstname = "Bob ");

(Of course, if there is a reference to the set of employee, no cast is needed. Here is just an example)

Similarly, cast It can be used on ComboBox, datagridview, and treenode:

1 // ComboBox
2 var V1 = ComboBox. Items. Cast ();
3 // datagridview
4 var v2 = datagridview. selectedrows. Cast ();
5 var V3 = datagridview. selectedcolumns. Cast ();
6 var V4 = datagridview. selectedcells. Cast ();
7 // treenode
8 var V5 = treenode. nodes. Cast
();

Among these applications, the maximum number of applications is 4th rows. Obtaining the selected row is one of the most frequently used operations of the dview.

The following code is used:

1 // calculate the average age
2 int age = datagridview. selectedrows. Cast (). Average (P => P. Age );
3 // city where statistics are made
4 string [] cities = datagridview. selectedrows. Cast (). Select (P => P. City). Distinct ();

Cast is used. , Our code is very streamlined.

Cast It can even be used on the basic class control of all controls. Its CONTROLS attribute is also non-generic!

1 // Control
2 var V6 = control. Controls. Cast ();

Related Article

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.