I would like to programmatically add or remove some elements to a string array in C#, but still keeping the items I had before, a bit like the VB function ReDim Preserve.
-
8What have you tried? This question due to it's basic nature will probably have 50 answers in 3 minutes. Google would have been just as helpful. – Erik Philips Mar 05 '12 at 17:17
-
2vb's redim preserve was and is evil. You're are **always**, without exception, better off using a collection type. And it's not often I will make absolute declarations like that. – Joel Coehoorn Mar 05 '12 at 17:19
-
2Minus 5 seems a little harsh for this question. I struggled with this concept and transition upon moving from VBA to C#. – Brad Mar 05 '12 at 17:51
-
1Duplicate of http://stackoverflow.com/questions/202813/adding-values-to-a-c-sharp-array – Omar Mar 05 '12 at 18:03
10 Answers
The obvious suggestion would be to use a List<string>
instead, which you will have already read from the other answers. This is definitely the best way in a real development scenario.
Of course, I want to make things more interesting (my day that is), so I will answer your question directly.
Here are a couple of functions that will Add and Remove elements from a string[]
...
string[] Add(string[] array, string newValue){
int newLength = array.Length + 1;
string[] result = new string[newLength];
for(int i = 0; i < array.Length; i++)
result[i] = array[i];
result[newLength -1] = newValue;
return result;
}
string[] RemoveAt(string[] array, int index){
int newLength = array.Length - 1;
if(newLength < 1)
{
return array;//probably want to do some better logic for removing the last element
}
//this would also be a good time to check for "index out of bounds" and throw an exception or handle some other way
string[] result = new string[newLength];
int newCounter = 0;
for(int i = 0; i < array.Length; i++)
{
if(i == index)//it is assumed at this point i will match index once only
{
continue;
}
result[newCounter] = array[i];
newCounter++;
}
return result;
}

- 47,875
- 21
- 135
- 185
-
Thanks everybody for the time spent on my question and for having answered to it!!!! Everyone of you has been useful to me!!! Thanks in particular to you, your answer is the one I was looking for!!! – Cippo Mar 05 '12 at 17:37
-
Note that a `List
` does exactly the same internally as `ReDim Preserve` would do on an array. In fact `List – Olivier Jacot-Descombes Aug 04 '20 at 15:12` stores things in an internal array and resizes it automatically when required. This makes `ReDim Preserve` obsolete even in VB.
If you really won't (or can't) use a generic collection instead of your array, Array.Resize is c#'s version of redim preserve:
var oldA = new [] {1,2,3,4};
Array.Resize(ref oldA,10);
foreach(var i in oldA) Console.WriteLine(i); //1 2 3 4 0 0 0 0 0 0

- 24,914
- 3
- 72
- 86
Don't use an array - use a generic List<T>
which allows you to add items dynamically.
If this is not an option, you can use Array.Copy
or Array.CopyTo
to copy the array into a larger array.

- 489,969
- 99
- 883
- 1,009
Since arrays implement IEnumerable<T>
you can use Concat
:
string[] strArr = { "foo", "bar" };
strArr = strArr.Concat(new string[] { "something", "new" });
Or what would be more appropriate would be to use a collection type that supports inline manipulation.

- 18,753
- 6
- 54
- 84
Use List<string>
instead of string[]
.
List allows you to add and remove items with good performance.

- 43,130
- 20
- 110
- 148
What's abaut this one:
List<int> tmpList = intArry.ToList();
tmpList.Add(anyInt);
intArry = tmpList.ToArray();

- 530
- 3
- 17
One liner:
string[] items = new string[] { "a", "b" };
// this adds "c" to the string array:
items = new List<string>(items) { "c" }.ToArray();

- 556
- 7
- 23
You can use this snippet:
static void Main(string[] args)
{
Console.WriteLine("Enter number:");
int fnum = 0;
bool chek = Int32.TryParse(Console.ReadLine(),out fnum);
Console.WriteLine("Enter number:");
int snum = 0;
chek = Int32.TryParse(Console.ReadLine(),out snum);
Console.WriteLine("Enter number:");
int thnum = 0;
chek = Int32.TryParse(Console.ReadLine(),out thnum);
int[] arr = AddToArr(fnum,snum,thnum);
IOrderedEnumerable<int> oarr = arr.OrderBy(delegate(int s)
{
return s;
});
Console.WriteLine("Here your result:");
oarr.ToList().FindAll(delegate(int num) {
Console.WriteLine(num);
return num > 0;
});
}
public static int[] AddToArr(params int[] arr) {
return arr;
}
I hope this will help to you, just change the type

- 280
- 2
- 4
You can use a generic collection, like List<>
List<string> list = new List<string>();
// add
list.Add("element");
// remove
list.Remove("element");

- 1,679
- 2
- 20
- 27
-
The equivalent concept of removing an element from as list as from an array is probably through the `RemoveAt` method because it takes in index value. `Remove` removes the first occurrence which might be desired but seems trickier to control. – Brad Mar 05 '12 at 17:57