Solved

Convert from IList<T> to List<T>

Posted on 2010-11-14
4
675 Views
Last Modified: 2013-12-17
Dear All,


Sometimes this simple conversion works: (in C# )


List<T> result = ilistobject as List<T>;



But sometimes it may give me NULL result and I have to use this:


List<T> result = ilistobject.OfType<T>().ToList<T>();
 

How come?

Thanks a lot!
0
Comment
Question by:chenyuhao88
4 Comments
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 150 total points
ID: 34133315
Those are NOT the same operations. In the first, you are attempting to cast an object to a List<T> and if the object is not compatible with a List<T> you are given NULL instead. In the second, you are filtering your list for only objects of type T (OfType<T>) and then returning a list of those objects (ToList).
0
 
LVL 13

Accepted Solution

by:
Naman Goel earned 200 total points
ID: 34134039
System.Collections.Generic.IList<T> is an Interface which is implemented in following System classes
List<T>, SortedList<TKey,TValue>,System.Collections.ObjectModel.Collection<T>, System.Collections.ObjectModel.ReadOnlyCollection<T>

So if we are having a different type of collection object in ilistobject which is not compatible with List<T> then


List<T> result = ilistobject as List<T>;
result will be null.

and  ilistobject.OfType<T>().ToList<T>(); will filter the ilistobject based on type T.

so if we are having an IList<Object>


            IList<object> ilistobject = new object[] { "abcd", 2, 3 };

            List<String> result = ilistobject.OfType<string>().ToList<string>();

we will get abcd in result.

0
 
LVL 35

Assisted Solution

by:Miguel Oz
Miguel Oz earned 150 total points
ID: 34140241
IList<T> supports IEnumerable interface, just pass ilistobject to the list constructor.
IList<string> ilistobject = new string[] { "abcd", "test", "pass" };
List<String> result = new List<string>(ilistobject);

You can add an extra check depending on how your overall code is structured:
            if (ilistobject is IList<string>)
            {
                List<String> result = new List<string>(ilistobject);
            }
       
0
 

Author Closing Comment

by:chenyuhao88
ID: 34161865
Thanks guys!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now