You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* clarify allowed conversions
Enumerable.Cast only performs certain classes of conversions. Enumerate which are eligible in the reference.
* add a recommendation for select clause
The Func used in the select clause can use other categories of conversions.
* sweep for term "cast"
Prefer conversion
* re-write and simplify
The first draft was bad.
I like this one much better.
* final feedback
Copy file name to clipboardExpand all lines: xml/System.Linq/Enumerable.xml
+20-4Lines changed: 20 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1765,7 +1765,11 @@
1765
1765
1766
1766
The <xref:System.Linq.Enumerable.Cast%60%601%28System.Collections.IEnumerable%29> method enables the standard query operators to be invoked on non-generic collections by supplying the necessary type information. For example, <xref:System.Collections.ArrayList> does not implement <xref:System.Collections.Generic.IEnumerable%601>, but by calling <xref:System.Linq.Enumerable.Cast%60%601%28System.Collections.IEnumerable%29> on the <xref:System.Collections.ArrayList> object, the standard query operators can then be used to query the sequence.
1767
1767
1768
-
If an element cannot be cast to type `TResult`, this method will throw an exception. To obtain only those elements that can be cast to type `TResult`, use the <xref:System.Linq.Enumerable.OfType%2A> method instead of <xref:System.Linq.Enumerable.Cast%60%601%28System.Collections.IEnumerable%29>.
1768
+
If an element cannot be converted to type `TResult`, this method throws a <xref:System.InvalidCastException>.
1769
+
1770
+
The source sequence for this method is <xref:System.Collections.IEnumerable>, which means the elements have the compile-time static type of `object`. The only type conversions that are performed by this method are reference conversions and unboxing conversions. The runtime type of the elements in the collection must match the target type, or in the case of value types, the runtime type of elements must be the result of a boxing conversion of the target type. Other conversion types, such as those between different numeric types, are not allowed.
1771
+
1772
+
To obtain only those elements that can be converted to type `TResult`, use the <xref:System.Linq.Enumerable.OfType%2A> method instead of <xref:System.Linq.Enumerable.Cast%60%601%28System.Collections.IEnumerable%29>.
1769
1773
1770
1774
In a query expression, an explicitly typed iteration variable translates to an invocation of <xref:System.Linq.Enumerable.Cast%60%601%28System.Collections.IEnumerable%29>. This example shows the syntax for an explicitly typed range variable.
1771
1775
@@ -1776,9 +1780,21 @@ from int i in objects
1776
1780
```vb
1777
1781
From i As Integer In objects
1778
1782
```
1779
-
1780
-
1781
-
1783
+
1784
+
Use the `select` clause of a query to perform other conversion types, like the implicit numeric conversions. The following example uses both the `Cast` method and a `select` statement to convert a sequence of boxed integers to a sequence of doubles.
1785
+
1786
+
```csharp
1787
+
IEnumerable sequence = Enumerable.Range(0, 10);
1788
+
var doubles = from int item in sequence
1789
+
select (double)item;
1790
+
```
1791
+
1792
+
```vb
1793
+
Dim sequence As IEnumerable = Enumerable.Range(0, 10)
1794
+
Dim doubles = From item As Integer In sequence
1795
+
Select CType(item, Double)
1796
+
```
1797
+
1782
1798
## Examples
1783
1799
The following code example demonstrates how to use <xref:System.Linq.Enumerable.Cast%60%601%28System.Collections.IEnumerable%29> to enable the use of the standard query operators on an <xref:System.Collections.ArrayList>.
0 commit comments