How C # Converts list of object to list of T concrete types

Source: Internet
Author: User

Last week the code encountered a problem, because of design constraints, a method accepts parameters only list<object> type, but the method needs to deal with the real data is determined list<currency>. However, C # does not allow a direct conversion type to be displayed and is not operable in two directions. The problem blew me off for a while, and finally, on MSDN, I found a oftype<t> way to do it.

Enumerable.oftype<tresult> Method:

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 operation. The query represented by this method is executed only when the object is enumerated by invoking the GetEnumerator method of the object directly or by using foreach in Visual C # (or for each in visual Basic).

The Oftype<tresult> (IEnumerable) method returns only those elements in source that can be converted to TResult types. If an element cannot be converted to a TResult type, but does not want to receive an exception, use Cast<tresult> (IEnumerable).

This method is one of the few standard query operator methods that can be applied to a collection that contains non-parameterized types such as ArrayList. This is because oftype<tresult> extends the type IEnumerable. Oftype<tresult> not only cannot be applied to collections based on parameterized ienumerable<t> types, nor can they be applied to collections based on IEnumerable types that are not parameterized.

By applying oftype<tresult> to a collection that implements IEnumerable, you get the ability to query a collection using standard query operators. For example, specifying the type parameter of object as Oftype<tresult> will return an object whose type is Visual in ienumerable<object> or IEnumerable (of object) in C # Basic, the standard query operator can be applied to the object.

Let's look at a visual example:

Static void Main(String[]Args){ List<object>Currencylist= New List<object>() { New Currency(){Id = Guid.NewGuid(), Name = A}, New Currency(){Id = Guid.NewGuid(), Name = "B"}, New Currency(){Id = Guid.NewGuid(), Name = C} }; List<object>CurrencyList1= New List<object>() { A, "B", C }; List<Currency>Currencies=Currencylist.OfType<Currency> ().ToList(); List<Currency>Currencies1=CurrencyList1.OfType<Currency> ().ToList(); Console.WriteLine("Currencies list:"); Foreach (VarItemInchCurrencies) { Console.WriteLine(Item.id);  } console. Writeline "currencies1 list:" foreach  (var item in Currencies1)  {  console. Writeline (item. Id }}       /span>                

In this code, currencies is a valid currency object, so it can be converted. The Currencies1 is a string object, not a currency, so it cannot be converted or thrown. The result of the last Currencies1 is an empty list, not null. The output of this code is as follows:

How C # Converts list of object to list of T concrete types

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.