tags:

views:

1902

answers:

3

I've got a string array in C# and I want to pop the top element off the array (ie. remove the first element, and move all the others up one). Is there a simple way to do this in C#? I can't find an Array.Pop method.

Would I need to use something like an ArrayList? The order of the items in my array is important.

+12  A: 

Use a List, Queue or Stack instead..

List<String>
Queue<String>
Stack<String>
driAn
+2  A: 

From MSDN:

Code Block
using System;

using System.Collections.Generic;



namespace ConsoleApplication1

{
    class MSDNSample
    {
       static void Main()
       {
          string input = "a b c d";

          Stack<string> myStack = new Stack<string>(
             input.Split(new string[] { " " }, StringSplitOptions.None));

          // Remove the top element (will be d!)
          myStack.Pop();

          Queue<string> myQueue = new Queue<string>(

          input.Split(new string[] { " " }, StringSplitOptions.None));

          // Remove the first element (will be a!)
          myQueue.Dequeue();

       }
    }
}

http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/a924097e-3d72-439d-984a-b371cd10bcf4/

Paul Lefebvre
+6  A: 

Queue<T> (first in, first out) or Stack<T> (last in, first out) are what you're after.

Arrays in .NET are fixed length - you can't remove an element from them or indeed add elements to them. You can do this with a List<T> but Queue<T> and Stack<T> are more appropriate when you want queue/stack semantics.

Jon Skeet
LIFOFIFOLIFOFIFOLIFOFIFOLIFOFIFOLIFOFIFOLIFOFIFOLIFOFIFOLIFOFIFO... sorry you triggered my OCD.
Will