Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C#. Delete a known element from an array.

Options
  • 05-07-2012 3:51pm
    #1
    Registered Users Posts: 7,501 ✭✭✭


    I have the need to delete an element from an array. The element will always be the last in the array.

    I could just create a new array and move only the elements i want into it or is there an easier way?


Comments

  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    Set the element to null, let the garbage collector do it's thing. But that would not resize the array.

    If you want to do that, use a different data structure, like, say, List<T> and the RemoveAt function.

    In fact, take a look at all the generic collection classes here. You should know them very well. Arrays are great and all, but they're limited.

    Data structures and string manipulation. The two things I look at first when learning a new language / framework.


  • Registered Users Posts: 7,501 ✭✭✭BrokenArrows


    Set the element to null, let the garbage collector do it's thing. But that would not resize the array.

    If you want to do that, use a different data structure, like, say, List<T> and the RemoveAt function.

    In fact, take a look at all the generic collection classes here. You should know them very well. Arrays are great and all, but they're limited.

    Data structures and string manipulation. The two things I look at first when learning a new language / framework.

    Thanks.

    The List is alot more flexible.


  • Closed Accounts Posts: 2,930 ✭✭✭COYW


    Thanks.

    The List is alot more flexible.

    List is definitely the way to go. Use .ToList<>() to convert your array and then you can remove the element using RemoveAt().


  • Registered Users Posts: 543 ✭✭✭solarith


    I guess Arrays are more lightweight, but OP probably not worrying about that.


Advertisement