addinng a new element to the array of strings

  • Thread starter Thread starter Anatol Ciolac
  • Start date Start date
You could always create some kind so static util method that could
handle this; something like...


public static string[] addStringElement( string[] oldArray, string
element )
{
String[] newArray = new String[ oldArray.Length + 1 ];
Array.Copy( oldArray, newArray, oldArray.Length );
newArray[ oldArray.Length ] = element;
return newArray;
}

This may not be as fast as the ArrayList, but would keep the memory
consumption tight...

~harris
 
Harris said:
You could always create some kind so static util method that could
handle this; something like...

public static string[] addStringElement( string[] oldArray, string
element )
{
String[] newArray = new String[ oldArray.Length + 1 ];
Array.Copy( oldArray, newArray, oldArray.Length );
newArray[ oldArray.Length ] = element;
return newArray;
}

This may not be as fast as the ArrayList, but would keep the memory
consumption tight...

Well, it would keep the absolute memory consumption tight, but in
creating more arrays in the first place, it would cause a lot more
memory "churn". It would be far better to use ArrayList while expanding
and then call TrimToSize when everything had been added, if you're
really worried about the extra size.
 
Back
Top