C# 中的“List.Remove”不删除项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10971167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"List.Remove" in C# does not remove item?
提问by vivid
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;
采纳答案by Ricardo Souza
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 IEquatableinterface 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.
只是为了澄清这里的地址,正如 pst 所指出的,您还可以实现IEquatable接口和一些覆盖,例如 Groo 的答案以使其工作,但我认为这在这个特定主题上有点过头了。将此作为最常见的做法,但要明确指出也可以从列表中删除项目,即使它们是不同的实例,甚至是使用类似技术的不同对象。
回答by Oded
You are creating a newUsersobject - 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.
您正在创建一个新Users对象 - 这与已经存在的任何对象都不相同Application["Users_On"](它将具有不同的引用),因此不会被删除。
This assumes that Equalsand/or IEquatable<T>were not overridden/implemented in Users.
这假设Equals和/或IEquatable<T>没有在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.
顺便说一句 - 你的变量命名不是很好 - 选择更具描述性的名称。
回答by Groo
By default, object equality is compared by reference in .NET (unless Equalsis overriden, every object inherits from object.Equals). If you want the Removemethod to find your object, you cannot pass a new object.
默认情况下,对象相等性在 .NET 中通过引用进行比较(除非Equals被覆盖,每个对象都继承自object.Equals)。如果您希望该Remove方法找到您的对象,则不能传递新对象。
The simplest way would be to find the actual objectwhich 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);
}
回答by Omar
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;
回答by Robert
Remove it in place by finding the item inside the remove statement, not via an additional copy:
通过在 remove 语句中查找项目而不是通过额外的副本将其删除:
List<Users> us = ((List<Users>)Application["Users_On"]);
us.Remove(us.FirstOrDefault(u => u.ID == (int)Session["Current_Id"]));
Application["Users_On"] = us;
回答by Jakub
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.
正如有人在之前的答案中所说:在 .NET 中通过引用比较对象相等性。但是您可以通过简单地将 List 中的元素 T 从类转换为结构体来受益于类和结构体之间的差异。

