.NET Framework Class Library
EnumerableOfTypeTResult Method

Filters the elements of an IEnumerable based on a specified type.

Namespace:   System.Linq
Assemblies:   System.Linq (in System.Linq.dll)
  System.Core (in System.Core.dll)
Syntax
<[%$TOPIC/bb360913_en-us_VS_110_2_0_0_0_0%]> _
Public Shared Function OfType(Of TResult) ( _
	source As [%$TOPIC/bb360913_en-us_VS_110_2_0_0_0_1%] _
) As [%$TOPIC/bb360913_en-us_VS_110_2_0_0_0_2%](Of TResult)
public static [%$TOPIC/bb360913_en-us_VS_110_2_0_1_0_0%]<TResult> OfType<TResult>(
	this [%$TOPIC/bb360913_en-us_VS_110_2_0_1_0_1%] source
)
[[%$TOPIC/bb360913_en-us_VS_110_2_0_2_0_0%]]
public:
generic<typename TResult>
static [%$TOPIC/bb360913_en-us_VS_110_2_0_2_0_1%]<TResult>^ OfType(
	[%$TOPIC/bb360913_en-us_VS_110_2_0_2_0_2%]^ source
)
static member OfType : 
        source:[%$TOPIC/bb360913_en-us_VS_110_2_0_3_0_0%] -> [%$TOPIC/bb360913_en-us_VS_110_2_0_3_0_1%]<'TResult> 
JScript does not support generic types or methods.

Type Parameters

TResult

The type to filter the elements of the sequence on.

Parameters

source
Type: System.CollectionsIEnumerable

The IEnumerable whose elements to filter.

Return Value

Type: System.Collections.GenericIEnumerableTResult
An IEnumerableT that contains elements from the input sequence of type TResult.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerable. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Exceptions
ExceptionCondition
ArgumentNullException

source is .

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 Visual C# or For Each in Visual Basic.

The OfTypeTResult(IEnumerable) method returns only those elements in source that can be cast to type TResult. To instead receive an exception if an element cannot be cast to type TResult, use CastTResult(IEnumerable).

This method is one of the few standard query operator methods that can be applied to a collection that has a non-parameterized type, such as an ArrayList. This is because OfTypeTResult extends the type IEnumerable. OfTypeTResult cannot only be applied to collections that are based on the parameterized IEnumerableT type, but collections that are based on the non-parameterized IEnumerable type also.

By applying OfTypeTResult to a collection that implements IEnumerable, you gain the ability to query the collection by using the standard query operators. For example, specifying a type argument of Object to OfTypeTResult would return an object of type IEnumerable<Object> in C# or IEnumerable(Of Object) in Visual Basic, to which the standard query operators can be applied.

Examples

The following code example demonstrates how to use OfTypeTResult to filter the elements of an IEnumerable.

        ' Create an ArrayList and add items to it. 
        Dim fruits As New System.Collections.ArrayList(4)
        fruits.Add("Mango")
        fruits.Add("Orange")
        fruits.Add("Apple")
        fruits.Add(3.0)
        fruits.Add("Banana")

        ' Apply OfType(Of String)() to the ArrayList 
        ' to filter out non-string items. 
        Dim query1 As IEnumerable(Of String) = fruits.OfType(Of String)()

        ' Print the results. 
        Dim output As New System.Text.StringBuilder("Elements of type 'string' are:" _
                                                    & vbCrLf)
        For Each fruit As String In query1
            output.AppendLine(fruit)
        Next 

        ' The following query shows that the standard query operators such as  
        ' Where() can be applied to the ArrayList type after calling OfType(). 
        Dim query2 As IEnumerable(Of String) = _
            fruits.OfType(Of String)().Where(Function(fruit) _
                                                 fruit.ToLower().Contains("n"))

        output.AppendLine(vbCrLf & "The following strings contain 'n':")
        For Each fruit As String In query2
            output.AppendLine(fruit)
        Next 

        ' Display the output.
        MsgBox(output.ToString())

        ' This code produces the following output: 
        ' 
        ' Elements of type 'string' are: 
        ' Mango 
        ' Orange 
        ' Apple 
        ' Banana 
        ' 
        ' The following strings contain 'n': 
        ' Mango 
        ' Orange 
        ' Banana
            System.Collections.ArrayList fruits = new System.Collections.ArrayList(4);
            fruits.Add("Mango");
            fruits.Add("Orange");
            fruits.Add("Apple");
            fruits.Add(3.0);
            fruits.Add("Banana");

            // Apply OfType() to the ArrayList.
            IEnumerable<string> query1 = fruits.OfType<string>();

            Console.WriteLine("Elements of type 'string' are:");
            foreach (string fruit in query1)
            {
                Console.WriteLine(fruit);
            }

            // The following query shows that the standard query operators such as  
            // Where() can be applied to the ArrayList type after calling OfType().
            IEnumerable<string> query2 =
                fruits.OfType<string>().Where(fruit => fruit.ToLower().Contains("n"));

            Console.WriteLine("\nThe following strings contain 'n':");
            foreach (string fruit in query2)
            {
                Console.WriteLine(fruit);
            }

            // This code produces the following output: 
            // 
            // Elements of type 'string' are: 
            // Mango 
            // Orange 
            // Apple 
            // Banana 
            // 
            // The following strings contain 'n': 
            // Mango 
            // Orange 
            // Banana
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8