Have you ever tried to remove an object from a collection and the collection still has this object? The world is big and wonders. A slight negligence will lead to this strange phenomenon. Now let's see what this "Undead" object is all about.
1, "Not Dead" object appeared
This question was originally proposed by one of my colleagues, to reproduce the "undead" object, the code is simplified as follows:
Code #01
IList products = new List<Product>();
products.Add(GetProduct("1412"));
products.Remove(GetProduct("1412"));
Where the Product class code is as follows:
Code #02
class Product
{
public Product(string id)
{
m_ID = id;
}
private string m_ID;
public string ID
{
get { return m_ID; }
}
public override string ToString()
{
return "ID: " + m_ID;
}
}
The GetProduct method reads the data from the database and returns it based on the incoming ID, and its signature is as follows:
Code #03
public static Product getproduct (string id);
To see whether an object with number 1412 is removed from the products, simply add one line to the end of the code #01:
Code #04
Console.WriteLine (Products. Count);