In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The content of this article mainly focuses on what is the use of Enumerable.Cast. The content of the article is clear and well-organized. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!
Enumerable.Cast
< T>Used to convert IEnumerable to a generic version of IEnumerable
< T>. After conversion, you can enjoy other methods of Enumerable (such as Where, Select), which brings great convenience to our coding.
But only one example of converting ArrayList is given in MSDN, and many people read it and feel that they are using List now.
< T>Who else can use ArrayList,Cast?
< T>It's not very useful unless you deal with some of the code that was left over before.
Actually, Cast
< T>It's not that simple. It can be used in many places.
Enumerable.Cast
< T>Use example
Let's take a look at the example in MSDN:
System.Collections.ArrayList fruits = new System.Collections.ArrayList (); fruits.Add ("apple"); fruits.Add ("mango"); IEnumerable
< string>Query = fruits.Cast
< string>(); foreach (string fruit in query) Console.WriteLine (fruit)
This example is relatively simple and easy to understand.
Similarly, several other collection classes in .net 1.x can also be used as above, such as Array, non-generic version of List...
Excuse me, do you have a non-generic version of List? I haven't used .net 1.x very much, I don't know, but there is a List control (ASP.Net) and a ListView control (WinForm) in the form control.
Take ListView as an example. The ListView control can contain many items, or it can be said to be a collection. Let's take a look at its Items property.
Public class ListView: Control {public ListView.ListViewItemCollection Items {get;} public class ListViewItemCollection: IList, ICollection, IEnumerable {}}
The Items type of ListView is ListView.ListViewItemCollection, and this ListViewItemCollection implements IEnumerable.
ListView.Items is a collection of non-generics, so you can apply Cast
< T>.
The following code assumes that listBox data is bound to a collection of Employee:
Int count = listBox.Items.Cast
< Employee>(). Count (); bool b = listBox.Items.Cast
< Employee>Any (e = > e.FirstName = = "Bob")
(of course, if you have a reference to the collection of Employee, you don't need Cast. This is just an example.)
Same Cast
< T>Can be used on ComboBox, DataGridView, TreeNode:
/ / ComboBox var v1 = comboBox.Items.Cast
< People>(); / / DataGridView var v2 = dataGridView.SelectedRows.Cast
< DataGridViewRow>(); var v3 = dataGridView.SelectedColumns.Cast
< DataGridViewColumn>(); var v4 = dataGridView.SelectedCells.Cast
< DataGridViewCell>(); / / TreeNode var v5 = treeNode.Nodes.Cast
< TreeNode>()
Line 4 should be the most frequently used of these applications, and getting selected rows is one of the most frequently used operations in DataGridView.
Consider the following code:
/ / calculated average age int age = dataGridView.SelectedRows.Cast
< Employee>(). Average (p = > p.Age); / / string [] cities = dataGridView.SelectedRows.Cast in the city where the statistics are made
< Employee>(). Select (p = > p.City). Distinct ()
Cast is used
< T>Our code is very concise.
Enumerable.Cast
< T>Used on the base class Control
Cast
< T>It can even be used on the base class Control of all controls, whose Controls property is also non-generic!
/ / Control var V6 = control.Controls.Cast
< Control>()
Looks like Cast
< T>As if for Control, the Control class and the derived classes of Control use non-generics in many places.
But now they all use vs2008 (or even vs2010), so why do WinForm form controls still use non-generics? it's too backward!
Indeed, there are big problems with WinForm's support for generic controls (Control).
Although you can define generic controls, you can also use them and run them. But there will be a lot of trouble, such as the form designer can not display.
Then we have to use non-generic ones, but luckily we have Cast
< T>!
Let's take a look at Cast.
< T>For inheritance support, we define two classes An and BMageB that inherit from A, as follows:
Public class A {} public class B: A {}
Try the following type conversion operation:
/ / subclass collection B [] bb = new B [] {new B (), new B ()}; / / converted to parent class A [] aa = bb.Cast
< A>(). ToArray (); / / go back to subclass B [] bb2 = aa.Cast
< B>() ToArray ()
All the above three operations can be compiled and run. Modify them and try again:
A [] aa = new A [] {new A (), new A (), new B ()}; B [] bb3 = aa2.Cast
< B>() ToArray ()
Not this time, it's not random to make the parent class cast as a subclass:
But we have a solution. We use Enumerable.OfType.
< T>Oh, it's Cast.
< T>The brothers are used as follows:
B [] bb = aa.OfType
< B>() ToArray ()
After reading the above, I always feel like Cast.
< T>Is just a simple operation like (T) enumerator.Current, let's verify it again with int and double transformations:
Int I = (int) 1.001; double d = (double) 10; int [] ints1 = new int [] {1,2,3,4,5}; double [] ds1 = ints1.Cast
< double>(). ToArray (); double [] nums1 = new double [] {1.0001, 2.0003, 3.001, 3.9997, 4.002}; int [] nums2 = nums1.Cast
< int>() ToArray ()
1, 2 behavior to force type conversion, no problem. (of course, line 2 (double) can be omitted. )
Line 5 attempts to convert a collection of integers to a double collection, and the runtime reports an error:
Line 7 will report the same error. Looks like Cast
< T>The interior is not just a simple conversion!
Decompiled with Reflect, using the following class:
After decompilation, the code is quite messy, and my level is limited, and I don't understand it, so I'd better leave this problem to the master in the garden.
Enumerable.Cast
< T>Summary:
1. Cast
< T>It can be widely used in WinForm controls.
two。 On the collection transformation of classful inheritance, it is recommended to use OfType
< T>3. Cast
< T>Cannot be understood as simple type conversion.
Thank you for your reading. I believe you have a certain understanding of "what is the use of Enumerable.Cast". Go and practice it. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.