StringCollection.ToString() method

  • Thread starter Thread starter Naz
  • Start date Start date
N

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.StringCollection". 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
 
Naz said:
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.StringCollection". 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
 
Naz said:
Q: I thought stringCollection.ToString() will return all entries but it
return "System.Collections.Specialized.StringCollection". 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.
 
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);
 
"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.NewLine, myArr2, 0,
myArr2.Length));
 
Q: I thought stringCollection.ToString() will return all entries but it
return "System.Collections.Specialized.StringCollection". 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
 
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.
 
Naz said:
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.NewLine, 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
 
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
 
Back
Top