Solved

Convert from IList<T> to List<T>

Posted on 2010-11-14
4
685 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 36

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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

734 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