Hi,
I have class that has a property that is of type string[].
I need to create another array item, do I have to reset the size and copy all the previous items over?
Hi,
I have class that has a property that is of type string[].
I need to create another array item, do I have to reset the size and copy all the previous items over?
Yes. Arrays have fixed size.
From C# specification:
12.2 Array creation
...
When an array instance is created, the rank and length of each dimension are established and then remain constant for the entire lifetime of the instance. In other words, it is not possible to change the rank of an existing array instance, nor is it possible to resize its dimensions.
You may also take a look at blog post from Eric Lippert Arrays considered somewhat harmful
You can use Array.Resize
which will preserve the contents of the array.
e.g.
var array = myClass.SomeArrayProperty;
Array.Resize(ref array, newSize);
myClass.SomeArrayProperty = array;
You need to use a temporary variable because properties cannot be used as ref
arguments.
Yes, if you want to be able to dynamically append items use a List
.
Although it's a best practice to not expose List as a property, so use IList<string>
or IEnumerable<String>
instead.
If it's your class, you should probably change the property to use an IList<string>
, ICollection<string>
or IEnumerable<string>
, depending on how you anticipate this property will be used. Then, whichever of those you choose to return from the function, try using a List<string>
in the type itself.
I would prefer copying the string[] to List<string>, perform all the manipulations, then .ToArray() to produce the resulting string[].
Do you control the class? If so, consider using List<string>
internally instead.
(And while you're at it, consider using IList<string>
in your public API instead of string[]
.)
I would use a list if possible. It's a lot easier to manage. Obviously this depends on if it's your class and your able to chance this without any problems.
List<string> myList = new List<string>();
myList.Add("item");
myList.Remove("item");
If your having to use arrays then use
It is easier to work with List<T>
, but if you don't control the property this isn't an option. Note that this is seen commonly in soap-generated proxies, but IIRC there is a command-line switch you can use if you prefer lists to arrays.
Note that if you are doing this regularly, an extension method may be helpful:
public static T[] Append<T>(this T[] arr, T item) {
Array.Resize(ref arr, arr == null ? 1 : (arr.Length + 1));
arr[arr.Length-1] = item;
return arr;
}
Which you can then use for your property:
obj.SomeProp = obj.SomeProp.Append(someValue);
(note this works even if obj.SomeProp
is null, creating an array of length 1)
If it were a field/variable, then a simple ref
argument might also work:
public static void Append<T>(ref T[] arr, T item) {
Array.Resize(ref arr, arr == null ? 1 : (arr.Length + 1));
arr[arr.Length-1] = item;
}