Solved

Convert from IList<T> to List<T>

Posted on 2010-11-14
4
677 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 75

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A short film showing how OnPage and Connectwise integration works.

919 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

20 Experts available now in Live!

Get 1:1 Help Now