generic toarray string[] ?

S

Steph

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");
string.Join("','", (string[])al.ToArray(typeof(string)));

but with generic :
List<Guid> guid = new List<Guid>();
guid.Add(new Guid( /(..)/ ));
guid.Add(new Guid( /(..)/ ));
guid.Add(new Guid( /(..)/ ));
?? string.Join("','", (string[])guid.ToArray()); ??

thanks
 
J

Jon Skeet [C# MVP]

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");
string.Join("','", (string[])al.ToArray(typeof(string)));

Yes, that's converting a list of strings into an array of strings.
but with generic :
List<Guid> guid = new List<Guid>();
guid.Add(new Guid( /(..)/ ));
guid.Add(new Guid( /(..)/ ));
guid.Add(new Guid( /(..)/ ));
?? string.Join("','", (string[])guid.ToArray()); ??

That's trying to convert a list of Guids into an array of strings -
that's not going to work, and it wouldn't have worked if you'd had an
ArrayList of Guids either.

You could use List<Guid>.ConvertAll<string> and specify a delegate
which just calls ToString() on the Guid, then call ToArray.

Jon
 

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

Top