“List.Remove” in C# does not remove item?

Hello how can i remove item from generic list here is my code im trying to do it right but i dont know where i make mistake;/

Users us_end = new Users();
foreach (var VARIABLE in ((List<Users>)Application["Users_On"]))
{
    if(VARIABLE.Id == (int)Session["Current_Id"])
    {
        us_end.Name = VARIABLE.Name;
        us_end.Id = VARIABLE.Id;
        us_end.Data = VARIABLE.Data;
    }
}
List<Users> us = ((List<Users>)Application["Users_On"]);
us.Remove(us_end);
Application["Users_On"] = us;

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You have to get the same object to remove, not a copy.

Users us_end;

foreach (var VARIABLE in ((List<Users>)Application["Users_On"]))
{
    if(VARIABLE.Id == (int)Session["Current_Id"])
    {
       us_end = (Users)VARIABLE;
       break;
    }
}

if (us_end != null)
{
    List<Users> us = ((List<Users>)Application["Users_On"]);
    us.Remove(us_end);
    Application["Users_On"] = us;
}

Edit:

Just to clarify an address here, as pst pointed, you could also implement the IEquatable interface and some overridings like on the Groo’s answer to make it work, but i think it’s overkill on this specific subject. Giving this as the most common practice, but making clear that it’s also possible to remove items from a list, even if they are diferent instances or even diferent objects with a technique like that.

Ref.: http://msdn.microsoft.com/en-us/library/ms131187.aspx

Method 2

By default, object equality is compared by reference in .NET (unless Equals is overriden, every object inherits from object.Equals). If you want the Remove method to find your object, you cannot pass a new object.

The simplest way would be to find the actual object which has desired properties, and then remove it:

var id = (int)Session["Current_Id"];
var list = (List<Users>)Application["Users_On"];  

// find the exact item to remove.
var itemToRemove = list.FirstOrDefault(u => u.Id = id);

// if found, remove it
if (itemToRemove != null)
{
    list.Remove(itemToRemove);
}

Method 3

You are creating a new Users object – this is not the same as any object already in Application["Users_On"] (it will have a different reference), so it will not be removed.

This assumes that Equals and/or IEquatable<T> were not overridden/implemented in Users.

List<Users> us = ((List<Users>)Application["Users_On"]);
Users us_end = us.Where(u => u.Id == (int)Session["Current_Id"]).FirstOrDefault();
us.Remove(us_end);
Application["Users_On"] = us;

By the way – your variable naming is not very good – go for more descriptive names.

Method 4

What’s about:

List<Users> us = ((List<Users>)Application["Users_On"]);
Users us_end = us.First(x => x.ID == (int)Session["Current_Id"]);
us.Remove(us_end);
Application["Users_On"] = us;

Method 5

Remove it in place by finding the item inside the remove statement, not via an additional copy:

List<Users> us = ((List<Users>)Application["Users_On"]);
us.Remove(us.FirstOrDefault(u => u.ID == (int)Session["Current_Id"]));
Application["Users_On"] = us;

Method 6

As someone said in the previous answers: object equality is compared by reference in .NET.
But you can benefit from the difference between classes and structs by simply turning your element T inside List from class to struct.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x