Skip to content

Commit c5aae0f

Browse files
authored
Clarify enumerable cast (dotnet#4730)
* 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
1 parent ff37e47 commit c5aae0f

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

xml/System.Linq/Enumerable.xml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,7 +1765,11 @@
17651765

17661766
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.
17671767

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>.
17691773

17701774
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.
17711775

@@ -1776,9 +1780,21 @@ from int i in objects
17761780
```vb
17771781
From i As Integer In objects
17781782
```
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+
17821798
## Examples
17831799
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>.
17841800

0 commit comments

Comments
 (0)