Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

generic toarray string[] ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steph - 16 Oct 2007 10:21 GMT
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
Jon Skeet [C# MVP] - 16 Oct 2007 10:32 GMT
> hello, i ve tries to convert a generic into a string[] for use string.join.
>
[quoted text clipped - 4 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.