append character to string

M

mp

if i have a sequence of individual characters coming at me as inputs,
and i want to combine them into a string as output,
what's the preferrable implementation?
arraylist? stringbuilder? array? string?
number of characters is unknown so "array/collection" has to grow as needed


private void AddCharacterToString(char inputChar)
{
privateMemberObject.Add(inputChar);
or
privateMemberObject.Append(inputChar);
}
privateMemberObject.ToString() <--- final output

thanks
mark
 
M

mp

Peter Duniho said:
ArrayList is not preferable for anything now. It should be used only when
dealing with pre-generic APIs that require an ArrayList object. Otherwise,
if a general-purpose, array-backed list data structure is needed, the
generic List<T> should be used.

Arrays and strings are fixed-length, and as such they are very expensive
to use to maintain a variable-length data structure. Obviously they would
be a poor choice as well.

That leaves StringBuilder, which is in the fact effectively .NET's mutable
string type. It's specifically designed for the purpose of maintaining
strings that need to change over time, such as your exact scenario.

Pete

muchas gracias
mark
 

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