Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Convert from IList<T> to List<T>

Posted on 2010-11-14
4
Medium Priority
?
701 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 600 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 800 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 36

Assisted Solution

by:Miguel Oz
Miguel Oz earned 600 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Screencast - Getting to Know the Pipeline
Suggested Courses

972 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