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 / .NET Framework / New Users / April 2007

Tip: Looking for answers? Try searching our database.

List generic collection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AVL - 16 Apr 2007 13:28 GMT
hi,
i'm a new bie to .net 2.0..
ive used a list geeneric collection of strings in my code.

i would like to append all the strings to get a single value....
I've found the foreach() iteration method...for List collection..
any good sampels on how to use it and how is it beneficial than normal
iteration...
please help out!!!
Göran Andersson - 16 Apr 2007 17:19 GMT
> hi,
> i'm a new bie to .net 2.0..
[quoted text clipped - 5 lines]
> iteration...
> please help out!!!

To append all strings in a list, use a StringBuilder:

StringBuilder builder = new StringBuilder();
foreach (string s in list) {
   builder.Append(s);
}
string result = builder.ToString();

Above you also see the use of the foreach loop. For comparison, here's
how it's done with a regular for loop:

StringBuilder builder = new StringBuilder();
for (int i = 0; i < list.Count; i++) {
   builder.Append(list[i]);
}
string result = builder.ToString();

Which loop is better depends on what you want to do. One additional
advantage of the foreach loop is that it keeps track of the state of the
list you are iterating, so that if the list changes while you are
looping it, you get an exception.

Signature

Göran Andersson
_____
http://www.guffa.com


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.