469,927 Members | 1,835 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Converting an Array to a Collection

I'm having a problem converting an array to a collection. The code I'm
using looks like the following:

CustomerInfo[] cArray;
Collection<CustomerInfo> cCollection = new Collection<CustomerInfo>(cArray);

The above code creates a collection from the array but the collection winds
up as a read-only collection. Is there an easy way to convert an array to a
collection and have it not be read-only?

Thanks,

Chuck Cobb
Apr 14 '06 #1
5 10446
"Chuck Cobb" <ch********@oxfordcorp.com> ha scritto nel messaggio
news:%2******************@TK2MSFTNGP02.phx.gbl...
I'm having a problem converting an array to a collection. The code I'm
using looks like the following:

CustomerInfo[] cArray;
Collection<CustomerInfo> cCollection = new
Collection<CustomerInfo>(cArray);

The above code creates a collection from the array but the collection
winds up as a read-only collection. Is there an easy way to convert an
array to a collection and have it not be read-only?


If you don't have particular reasons to use a Collection<T> try with a
List<T>

--

Free .Net Reporting Tool - http://www.neodatatype.net
Apr 14 '06 #2
On Fri, 14 Apr 2006 15:35:12 -0400, "Chuck Cobb" <ch********@oxfordcorp.com>
wrote:
I'm having a problem converting an array to a collection. The code I'm
using looks like the following:

CustomerInfo[] cArray;
Collection<CustomerInfo> cCollection = new Collection<CustomerInfo>(cArray);

The above code creates a collection from the array but the collection winds
up as a read-only collection. Is there an easy way to convert an array to a
collection and have it not be read-only?

Thanks,

Chuck Cobb

From the VS 2k5 help:

string[] input = { "Brachiosaurus",
"Amargasaurus",
"Mamenchisaurus" };
List<string> dinosaurs = new List<string>(input);

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Apr 15 '06 #3
Otis Mukinfus <ph***@emailaddress.com> wrote:
From the VS 2k5 help:

string[] input = { "Brachiosaurus",
"Amargasaurus",
"Mamenchisaurus" };
List<string> dinosaurs = new List<string>(input);


If you can cope with IList instead of List, you don't need a new array:

IList<string> dinosaurs = input;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 15 '06 #4
Thanks! Using a List instead of a Collection works great!

I have no idea why a Collection doesn't work the same way...I've reported
that as a potential bug to Microsoft.
"Chuck Cobb" <ch********@oxfordcorp.com> wrote in message
news:%2******************@TK2MSFTNGP02.phx.gbl...
I'm having a problem converting an array to a collection. The code I'm
using looks like the following:

CustomerInfo[] cArray;
Collection<CustomerInfo> cCollection = new
Collection<CustomerInfo>(cArray);

The above code creates a collection from the array but the collection
winds up as a read-only collection. Is there an easy way to convert an
array to a collection and have it not be read-only?

Thanks,

Chuck Cobb

Apr 15 '06 #5


Chuck Cobb wrote:
Thanks! Using a List instead of a Collection works great!


Arrays can convert to IList<T>, IList, ICollection<T> and ICollection,
so this code will work:

string[] fruits = {"apple", "orange"};
ICollection<string> x1 = fruits;
ICollection x2 = fruits;
IList<string> x3 = fruits;
IList x4 = fruits;

Arrays are even recusively convertible:

string[] veggies = {"carrot", "salad"}
string[][] baskets = { fruits, veggies };

ICollection<ICollection<string>> x5 = baskets;

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Apr 19 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.