468,257 Members | 1,415 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

StringCollection.ToString() method

Naz
Hi,

I am new in C# and converting a program from delphi to C#. I have to
store a list of strings in memory and also need to save the strings to
disk at the end. I use StringCollection and my program looks like
this...

stringCollection.Add("line1");
stringCollection.Add("line2");
stringCollection.Add("line3");
....
....
foreach(string entry in stringCollection)
{
Console.WriteLine(entry);
}

Q: I thought stringCollection.ToString() will return all entries but it
return "System.Collections.Specialized.StringCollecti on". Instead
iterate through all entries, can I use any built-in method to return
all entries (like in delphi stringlist.text())?

Q: Is StringCollection is best for my program which is to store the
strings in memory and may save the strings to disk at the end?

Thanks

Nov 17 '05 #1
8 14756
Naz wrote:
Hi,

I am new in C# and converting a program from delphi to C#. I have to
store a list of strings in memory and also need to save the strings to
disk at the end. I use StringCollection and my program looks like
this...

stringCollection.Add("line1");
stringCollection.Add("line2");
stringCollection.Add("line3");
...
...
foreach(string entry in stringCollection)
{
Console.WriteLine(entry);
}

Q: I thought stringCollection.ToString() will return all entries but it
return "System.Collections.Specialized.StringCollecti on". Instead
iterate through all entries, can I use any built-in method to return
all entries (like in delphi stringlist.text())?

Q: Is StringCollection is best for my program which is to store the
strings in memory and may save the strings to disk at the end?

Thanks


If you just need the string appended together look at the stringbuilder
class. If you need them seperate a string collection is good or an
array of strings, depends how you need to access them.

Chris
Nov 17 '05 #2
Naz wrote:
Q: I thought stringCollection.ToString() will return all entries but it
return "System.Collections.Specialized.StringCollecti on". Instead
iterate through all entries, can I use any built-in method to return
all entries (like in delphi stringlist.text())?
Doesn't look it. I think you'll have to use StringCollection.CopyTo(),
and then String.Join().
Q: Is StringCollection is best for my program which is to store the
strings in memory and may save the strings to disk at the end?


It's OK. It's a strongly typed wrapper for an ArrayList, so Add() ops
are reasonably efficient.

--

www.midnightbeach.com
Nov 17 '05 #3
Naz
Thanks for your response. I don't think will add any value to copy
entries from string sollection into one-dimensional array of strings.
Strings array do not have any Join() method. Seems like I have iterate
though all entries. Here is the code to copy into strings array...

String[] myArray = new String[stringCollection.Count];
stringCollection.CopyTo(myArray, 0);

Nov 17 '05 #4
Naz
"Jon Shemitz", I misunderstood your suggestions. You proably suggest
the following code. The suggestions works fine. I don't have to iterate
through all entries in StringCollection. Thanks.

String[] myArr2 = new String[stringCollection.Count];
stringCollection.CopyTo(myArr2, 0);
Console.WriteLine(string.Join(System.Environment.N ewLine, myArr2, 0,
myArr2.Length));

Nov 17 '05 #5

"Naz" <na******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
<snip>
Q: I thought stringCollection.ToString() will return all entries but it
return "System.Collections.Specialized.StringCollecti on". Instead
iterate through all entries, can I use any built-in method to return
all entries (like in delphi stringlist.text())?


Hi,
I am not sure what you were doing wrong.
This works.
--------------------------------------------------------------------------
using System;
using System.Collections.Specialized;

class TEST
{

public static void Main(string[] args)
{
StringCollection sc = new StringCollection();
sc.Add("line1");
sc.Add("line2");
sc.Add("line3");

foreach(string entry in sc)
{
Console.WriteLine(entry);
}
}
}
------------------------------------------------------------
It produces :
line1
line2
line3

Hope this helps
Bill


Nov 17 '05 #6
Naz
Bill, I want to avoid iterate through the entries in stringcollection
and looking for any built in method which will return all entries. The
StringCollection in my program may have thousands of line.

Nov 17 '05 #7
"Naz" <na******@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Bill, I want to avoid iterate through the entries in stringcollection
and looking for any built in method which will return all entries. The
StringCollection in my program may have thousands of line.


Sorry, I misunderstood the issue.

Is there any reason particular reason you wish to avoid iterating through
the collection?
Any method that did it in one shot would be iterating internally anyway?

I don't see the point of this:
-----------------------------------------------------------------------------
String[] myArr2 = new String[stringCollection.Count];
stringCollection.CopyTo(myArr2, 0);
Console.WriteLine(string.Join(System.Environment.N ewLine, myArr2, 0,
myArr2.Length));
-----------------------------------------------------------------------------
over this
-----------------------------------------------------------------------------
foreach(string entry in stringCollection)
{
Console.WriteLine(entry);
}

-----------------------------------------------------------------------------
The explicit loop is much cleaner and easier to understand.

Are you concerned about performance?

I am confused
Bill
Nov 17 '05 #8
I believe what you want is
using System.Text;

StringBuilder stringCollection = new StringBuilder();

stringCollection.Append("line1");
stringCollection.Append(Environment.NewLine);
stringCollection.Append("line2");
stringCollection.Append(Environment.NewLine);
stringCollection.Append("line3");
stringCollection.Append(Environment.NewLine);

Console.Write(stringCollection.ToString());

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Naz" <na******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi,

I am new in C# and converting a program from delphi to C#. I have to
store a list of strings in memory and also need to save the strings to
disk at the end. I use StringCollection and my program looks like
this...

stringCollection.Add("line1");
stringCollection.Add("line2");
stringCollection.Add("line3");
...
...
foreach(string entry in stringCollection)
{
Console.WriteLine(entry);
}

Q: I thought stringCollection.ToString() will return all entries but it
return "System.Collections.Specialized.StringCollecti on". Instead
iterate through all entries, can I use any built-in method to return
all entries (like in delphi stringlist.text())?

Q: Is StringCollection is best for my program which is to store the
strings in memory and may save the strings to disk at the end?

Thanks

Nov 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jonathan | last post: by
2 posts views Thread by Dennis Myrén | last post: by
2 posts views Thread by Dan | last post: by
3 posts views Thread by Ben Fidge | last post: by
1 post views Thread by Juan Francisco Figueroa Perez | last post: by
reply views Thread by Dinesh Rathi | last post: by
3 posts views Thread by Rob | last post: by
2 posts views Thread by =?Utf-8?B?YmlsbCB0aWU=?= | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.