Enumerable.Cast<TResult>(IEnumerable) Method

Definition

Casts the elements of an IEnumerable to the specified type.

C#
public static System.Collections.Generic.IEnumerable<TResult> Cast<TResult>(this System.Collections.IEnumerable source);

Type Parameters

TResult

The type to cast the elements of source to.

Parameters

source
IEnumerable

The IEnumerable that contains the elements to be cast to type TResult.

Returns

IEnumerable<TResult>

An IEnumerable<T> that contains each element of the source sequence cast to the specified type.

Exceptions

source is null.

An element in the sequence cannot be cast to type TResult.

Examples

The following code example demonstrates how to use Cast<TResult>(IEnumerable) to enable the use of the standard query operators on an ArrayList.

C#
System.Collections.ArrayList fruits = new System.Collections.ArrayList();
fruits.Add("mango");
fruits.Add("apple");
fruits.Add("lemon");

IEnumerable<string> query =
    fruits.Cast<string>().OrderBy(fruit => fruit).Select(fruit => fruit);

// The following code, without the cast, doesn't compile.
//IEnumerable<string> query1 =
//    fruits.OrderBy(fruit => fruit).Select(fruit => fruit);

foreach (string fruit in query)
{
    Console.WriteLine(fruit);
}

// This code produces the following output:
//
// apple
// lemon
// mango

Remarks

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in C# or For Each in Visual Basic.

The Cast<TResult>(IEnumerable) method enables the standard query operators to be invoked on non-generic collections by supplying the necessary type information. For example, ArrayList does not implement IEnumerable<T>, but by calling Cast<TResult>(IEnumerable) on the ArrayList object, the standard query operators can then be used to query the sequence.

If an element cannot be converted to type TResult, this method throws a InvalidCastException.

The source sequence for this method is 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.

To obtain only those elements that can be converted to type TResult, use the OfType method instead of Cast<TResult>(IEnumerable).

In a query expression, an explicitly typed iteration variable translates to an invocation of Cast<TResult>(IEnumerable). This example shows the syntax for an explicitly typed range variable.

C#
from int i in objects

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.

C#
IEnumerable sequence = Enumerable.Range(0, 10);
var doubles = from int item in sequence
                select (double)item;

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

See also