C # Cast

Source: Internet
Author: User

In a form control, there is a list control (ASP. NET) and a ListView control (WinForm).

Take the ListView as an example, the ListView control can contain many items, or it can be said to be a collection, let's look at its Items property!

    1. public class listview:control{
    2. Public listview.listviewitemcollection Items {get;}
    3. public class Listviewitemcollection:ilist, ICollection, IEnumerable {
    4. }
    5. }

The items type of the ListView is Listview.listviewitemcollection, and this listviewitemcollection implements the IEnumerable. Listview.items is exactly a non-generic collection, so you can apply cast<t>. The following code assumes that the ListBox data is bound to a collection of employee:

  1. int count = listbox.items.cast <employee> (). Count ();  
  2. BOOL b = ListBox.Items.Cast<Employee>(). Any (e => e.firstname = = "Bob");

The same C # cast<t> can be used on ComboBox, DataGridView, TreeNode:

  1. ComboBox
  2. var v1 = ComboBox.Items.Cast<people>();
  3. DataGridView
  4. var v2 = dataGridView.SelectedRows.Cast<datagridviewrow>();
  5. var v3 = DataGridView.SelectedColumns.Cast<datagridviewcolumn>();
  6. var v4 = DataGridView.SelectedCells.Cast<datagridviewcell>()  ;
  7. TreeNode
  8. var v5 = TreeNode.Nodes.Cast<treeNode>();

These applications should have the largest number of applications in line 4th, and getting the selected row is one of the most frequently used operations for DataGridView. Sample the following code:

  1. Calculate average age
  2. int age = datagridview.selectedrows. Cast<Employee>().  Average (p=>p.age);
  3. City of Statistics
  4. String[] cities = datagridview.selectedrows. Cast<Employee>(). Select (p => p.city). Distinct ();

With C # Cast<t>, our code is very streamlined. Cast<t> can even be used on the base class control of all controls, and its controls property is non-generic!

    1. Control
    2. var v6 = control. Controls.cast<Control>();

It seems that C # cast<t> seems to be preparing for control, and the derived classes of the control class and control use non-generics in many places. can now use VS2008 (even vs2010), then why WinForm form control also use non-generic, too backward!!! Indeed, there is a big problem with WinForm support for generic controls. Although you can define a generic control, you can also use it to run. But there will be a lot of trouble, such as the form designer can not display ... That had to use the non-generic, fortunately we have C # cast<t>!

C # Cast

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.