Converting an Array to a Collection

  • Thread starter Thread starter Chuck Cobb
  • Start date Start date
C

Chuck Cobb

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
 
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>
 
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
 
Otis Mukinfus said:
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;
 
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 said:
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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top