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– Erik Philips2012-03-05 17:17:45 +00:00Commented Mar 5, 2012 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– Joel Coehoorn2012-03-05 17:19:33 +00:00Commented Mar 5, 2012 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– Brad2012-03-05 17:51:30 +00:00Commented Mar 5, 2012 at 17:51
-
1Duplicate of stackoverflow.com/questions/202813/…Omar– Omar2012-03-05 18:03:10 +00:00Commented Mar 5, 2012 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;
}
2 Comments
List<T> does exactly the same internally as ReDim Preserve would do on an array. In fact List<T> 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
Comments
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.
Comments
One liner:
string[] items = new string[] { "a", "b" };
// this adds "c" to the string array:
items = new List<string>(items) { "c" }.ToArray();
1 Comment
You should take a look at the List object. Lists tend to be better at changing dynamically like you want. Arrays not so much...
Comments
You can use a generic collection, like List<>
List<string> list = new List<string>();
// add
list.Add("element");
// remove
list.Remove("element");
1 Comment
RemoveAt method because it takes in index value. Remove removes the first occurrence which might be desired but seems trickier to control.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