Enumerable. OfType)>) Method

Source: Internet
Author: User

Filters IEnumerable elements based on the specified type.

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

 

This method is implemented by using delayed execution. The real-time return value is an object that stores all information required to perform operations. OnlyGetEnumeratorMethod or useForeach(Or in Visual BasicFor Each.

OfType <(Of <(TResult>) (IEnumerable)Method returns onlySourceWhich can be convertedTResultType element. If the element cannot be convertedTResultType, but do not want to receive exceptions, use Cast <(Of <(TResult>) (IEnumerable ).

This method is one of the few standard query operator methods. The standard query operator method can be applied to a set containing non-parameterized types (such as ArrayList. This is becauseOfType <(Of <(TResult>)Extended type IEnumerable.OfType <(Of <(TResult>)It cannot be applied to a set Of parameter-based IEnumerable <(Of <(T>)> or a set Of non-parametric IEnumerable types.

By addingOfType <(Of <(TResult>)Applies to the set that implements IEnumerable to obtain the ability to query sets using standard query operators. For example, specify the Object type parameterOfType <(Of <(TResult>)Returns an object of the Type C #IEnumerable <Object>OrIEnumerable (Of Object)The standard query operator can be applied to this object.

C # code:

Code
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
// 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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.