Export (0) Print
Expand All

Enumerable.OfType(Of TResult) Method

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

Namespace:  System.Linq
Assembly:  System.Core (in System.Core.dll)

'Declaration
<ExtensionAttribute> _
Public Shared Function OfType(Of TResult) ( _
	source As IEnumerable _
) As IEnumerable(Of TResult)

Type Parameters

TResult

The type to filter the elements of the sequence on.

Parameters

source
Type: System.Collections.IEnumerable
The IEnumerable whose elements to filter.

Return Value

Type: System.Collections.Generic.IEnumerable(Of TResult)
An IEnumerable(Of T) 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.

ExceptionCondition
ArgumentNullException

source is Nothing.

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 OfType(Of TResult)(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 Cast(Of TResult)(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 OfType(Of TResult) extends the type IEnumerable. OfType(Of TResult) cannot only be applied to collections that are based on the parameterized IEnumerable(Of T) type, but collections that are based on the non-parameterized IEnumerable type also.

By applying OfType(Of TResult) 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 OfType(Of TResult) 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.

The following code example demonstrates how to use OfType(Of TResult) to filter the elements of an IEnumerable.


      ' Create an ArrayList and add items to it.
      Dim fruits As New List(Of Object)(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.
      outputBlock.Text &= output.ToString() & vbCrLf

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



Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft