2
string[] columns

I want to delete the item on an index specified by a variable of type int.

How do I do this ?

I tried

columns.RemoveAt(MY_INT_HERE);

But apparently this does not works.

Anoushka Seechurn
  • 2,166
  • 7
  • 35
  • 52

6 Answers6

10

Array is immutable class, you can't change it, all you can do is to re-create it:

List<String> list = columns.ToList(); // <- to List which is mutable
list.RemoveAt(MY_INT_HERE);           // <- remove 
string[] columns = list.ToArray();    // <- back to array

May be the best solution is to redesign your code: change immutable array into List<String>:

  List<String> columns = ...
  columns.RemoveAt(MY_INT_HERE);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • .ToArray is NOT being accpted in my IDE why so ? – Anoushka Seechurn Dec 18 '13 at 11:48
  • @Anoushka Seechurn: I'm sorry, I've made a syntax error, List.RemoveAt returns void, not list; see my edit – Dmitry Bychenko Dec 18 '13 at 11:53
  • using `List` instead of `array` always is `not good` practice .. it `depends` of purpose ... always use `array` when you are performing a different `search` .. – Moumit Dec 18 '13 at 11:57
  • @Moumit Mondal: you're right, there's a trade off: array is more efficient, but more restricted than List so we should not change every array into List. In this particular case, however, List wins: array re-creating (if it occures often) is time consuming operation – Dmitry Bychenko Dec 18 '13 at 12:02
  • It's not quite right to say that arrays are '*immutable*'. The array members may change, but the size of the array is indeed fixed. – p.s.w.g Dec 18 '13 at 12:27
1

You cannot delete items in an array, because the length of a C# array is fixed at the time when it is created, and cannot be changed after that.

You can null out the corresponding element to get rid of the string, or use LINQ to produce a new array, like this:

columns = columns.Take(MY_INT_HERE-1).Concat(columns.Skip(MY_INT_HERE)).ToArray();

You need to add using System.Linq at the top of your C# file in order for this to compile.

However, using a List<string> would be a better solution:

List<string> columns;
columns.RemoveAt(MY_INT_HERE);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

If you don't want to use linq you can use this function :

public string[] RemoveAt(string[] stringArray, int index)
{
  if (index < 0 || index >= stringArray.Length)
    return stringArray;
  var newArray = new string[stringArray.Length - 1];
  int j = 0;
  for (int i = 0; i < stringArray.Length; i++)
  {
    if(i == index)continue;
    newArray[j] = stringArray[i];
    j++;
  }
  return newArray;
}

You use it like that : columns = RemoveAt(columns, MY_INT_HERE)

You can also make it to an extension method.

Swift
  • 1,861
  • 14
  • 17
0

Try one of the following (depending on what you need):

columns[MY_INT_HERE] = null;
columns[MY_INT_HERE] = string.Empty;

...otherwise you'll just have to create a new array which has a length of 1 less than your current array, and copy the values over.

If you want something more flexible, you might use a something like a List<string>, where you can use RemoveAt()

Kjartan
  • 18,591
  • 15
  • 71
  • 96
0

You can not delete it.You can recreate the array or I advice you to use List<string> for the same.

List<string> columns = new List<string>();
columns.RemoveAt(1);

It will remove the 2nd element from your List<String> columns

SPandya
  • 1,161
  • 4
  • 26
  • 63
0

Arrays are faster for the computer to work with but slower for a programmer. You will have to find that value with a loop or some other means, then set that position to null. You will end up with an empty space in the array. You could reallocate the array etc etc...

What is easier to use for relatively small amounts of data is a List. You can do myList.RemoveAt(100); and it will work nicely.

Daryl Van Sittert
  • 857
  • 12
  • 24