473,468 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ToArray() ?

Hi

I call a method which returns an IList containing a list of one type of
object. How do I best convert this IList to an array?

I can see that the ArrayList class has a ToArray() method - but this is not
part of IList.

(I do in fact know that the method I call actually returns an ArrayList as I
have looked in the source code, but technically I really only know it is an
IList).

Thanks,
Peter
Nov 17 '05 #1
4 13324
I call a method which returns an IList containing a list of one type of
object. How do I best convert this IList to an array?


IList list = ...
Foo[] foos = new Foo[list.Count];
for ( int i = 0; i < foos.Length; i++ )
foos[i] = (Foo)list[i];

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2

"Mattias Sjögren" <ma********************@mvps.org> skrev i en meddelelse
news:Oy**************@TK2MSFTNGP10.phx.gbl...
I call a method which returns an IList containing a list of one type of
object. How do I best convert this IList to an array?


IList list = ...
Foo[] foos = new Foo[list.Count];
for ( int i = 0; i < foos.Length; i++ )
foos[i] = (Foo)list[i];


Well, yeah. I just thought there might be an "in bult" method a la
ArrayList's ToArray().

Thanks,
Peter
Nov 17 '05 #3
How about using ICollection.CopyTo(...). IList implements ICollection.
"Peter Kirk" <pk@alpha-solutions.dk> wrote in message
news:ep**************@TK2MSFTNGP14.phx.gbl...

"Mattias Sjögren" <ma********************@mvps.org> skrev i en meddelelse
news:Oy**************@TK2MSFTNGP10.phx.gbl...
I call a method which returns an IList containing a list of one type of
object. How do I best convert this IList to an array?


IList list = ...
Foo[] foos = new Foo[list.Count];
for ( int i = 0; i < foos.Length; i++ )
foos[i] = (Foo)list[i];


Well, yeah. I just thought there might be an "in bult" method a la
ArrayList's ToArray().

Thanks,
Peter

Nov 17 '05 #4

"Peter Kirk" <pk@alpha-solutions.dk> wrote in message
news:u0**************@TK2MSFTNGP09.phx.gbl...
Hi

I call a method which returns an IList containing a list of one type of
object. How do I best convert this IList to an array?


You can load it into an ArrayList and uses ArrayLists.ToArray() if you
aren't worried about performance, otherwise I'd recommend writing a ToArray
implemetnation that takes an IList. Its not terribly hard to do, especially
if you know the type the list is going to be at all times.

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark Oueis | last post by:
ArrayList arrayList = new ArrayList(); foreach (Blabla bla in Blablas) { arrayList.Add(Convert.ToInt32(blabla.something)); } so far this works great.. but now
2
by: Sunny | last post by:
Hi, I have a small function, that returns a string, inside the function, there is an arraylist, being processed. At the end of the function all the elements of the ArrayList are copied to my...
2
by: Grey | last post by:
Dear all I want to know that how to use ToArray function in ArrayList. I have an ArrayList which is storing string, Actually, I want to use the Join method, so I think I need to convert the...
4
by: Derrick | last post by:
If i have an ArrayList, add a bunch of MyClass classes to it, how do I get a MyClass out of it? I've tried: MyClass clses = ArrayList.ToArray(typeof(MyClass)); but get errors about not being...
3
by: troy anderson | last post by:
I have a System.Collections.Queue instance filled with floats and I would like to convert it into a float because a third party plotting package requires it. I have tried casting Queue.ToArray()...
2
by: Dirk | last post by:
Hello The following code does not work ArrayList __gc* allControls = new ArrayList(); ..... // put some Controls in allControls .... Control __gc* arr = dynamic_cast<Control...
1
by: Tedmond | last post by:
Dear all, How can I convert a list of items from ArrayList into an array of non system type objects? e.g. The following codes have error: public class myClass { int i;
1
by: Steph | last post by:
hello, i ve tries to convert a generic into a string for use string.join. no problems with arraylist : ArrayList al = new ArrayList(); al.Add("J1"); al.Add("J2"); al.Add("J3");...
3
by: Michael Howes | last post by:
I have many double that each have a few thousand numbers in them. I need to concatenate groups of these double arrays into a new set of double. I don't know the total # of points. I thought it...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.