Overview C # cast

Source: Internet
Author: User

Overview C # cast
Www.firnow.com time: 2010-07-02 Author: Network Editor: mr. Abu CLICK: 50 [comment]
-
-
Here we use C # cast, and our code is simplified. Cast can even be used on the base class control of all controls. Its CONTROLS attribute is also non-generic!

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. public listview. listviewitemcollection items {Get;} 3. public class listviewitemcollection: ilist, icollection, ienumerable {4 .} 5 .} the items type of listview is listview. listviewitemcollection, which implements ienumerable. Listview. items is a non-generic set, so you can apply cast <t>. The following code assumes that the ListBox data is bound to a set of employees:

1. int COUNT = ListBox. items. cast <employee> (). count (); 2. bool B = ListBox. items. cast <employee> (). any (E => E. firstname = "Bob"); similarly, C # cast <t> can be used on ComboBox, datagridview, and treenode:

1. // ComboBox 2.var V1 = ComboBox. items. cast <people> (); 2. // datagridview 3.var v2 = datagridview. selectedrows. cast <datagridviewrow> (); 2.var V3 = datagridview. selectedcolumns. cast <datagridviewcolumn> (); 2.var V4 = datagridview. selectedcells. cast <datagridviewcell> (); 2. // treenode 3.var V5 = treenode. nodes. cast <treenode> (); 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 <employee> (). average (P => P. age); 2. // count the city 3. string [] cities = datagridview. selectedrows. cast <employee> (). select (P => P. city ). distinct (); C # cast <t> is used, and our code is simplified. Cast <t> can even be used on the base class control of all controls. Its CONTROLS attribute is also non-generic!

1. // control 2.var V6 = control. controls. cast <control> (); it seems that C # cast <t> is prepared for control. The control class and the control class use multiple non-generic classes. But now vs2008 (or even vs2010) is used. Why is it that the Form Control of winform is still non-generic? It is too backward !!! Indeed, winform's support for generic controls is very problematic. Although generic controls can be defined or used, they can be run. But there will be a lot of trouble, for example, the Form Designer cannot display... so we have to use a non-generic type. Fortunately, we have C # cast <t>!

Article Source: feino Network (www.firnow.com): http://dev.firnow.com/course/3_program/cshapo/csharpjs/20100702/319659.html

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.