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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C#. Delete a known element from an array.

  • 05-07-2012 2:51pm
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 2,040 ✭✭✭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,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, Registered Users 2 Posts: 543 ✭✭✭solarith


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


Advertisement