Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C#. Delete a known element from an array.

  • 05-07-2012 03:51PM
    #1
    Registered Users, Registered Users 2 Posts: 7,544 ✭✭✭


    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, Registered Users 2 Posts: 2,062 ✭✭✭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, Registered Users 2 Posts: 7,544 ✭✭✭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, Registered Users 2 Posts: 543 ✭✭✭solarith


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


Advertisement