Q:

c# List contain Item

I have a class named accessoire : 
   class accessoire 
    {
       public int value1 { get; set; }
        public string Value2 { get; set; }    
    }

then i have a List of that product
List<accessoire> accessoires

And i have an UI where the user pick the product he wants from a DataGridview and when he selected it launch  an event that add this item to the list : 
       private void ProductBrowser_OnItemAdded(Accessoire item)
        {

        if (Cart.Contains(item))
        {
            MessageBox.Show("Produit deja ajoutée au panier ! ");
        }
        else
        {
            Cart.Add(item);
            ProductView.Rows.Add(item.Ref, item.Name, Function.CatName(item.Cat), item.SellPrice, "1", Convert.ToDouble(item.SellPrice) * Convert.ToDouble(item.QtetoSell));
            TotalPriceSet();
            MessageBox.Show("Produit Ajouté !");
        }

    }

this doesnt work , but when i change it to : 
    private void ProductBrowser_OnItemAdded(Accessoire item)
    {
        var InList = Cart.Find(product => product.Ref == item.Ref);
        if (Cart.Contains(InList))
        {
            MessageBox.Show("Product already in list ! ");
        }
        else
        {
            Cart.Add(item);
            ProductView.Rows.Add(item.Ref, item.Name, Function.CatName(item.Cat), item.SellPrice, "1", Convert.ToDouble(item.SellPrice) * Convert.ToDouble(item.QtetoSell));
            TotalPriceSet();
            MessageBox.Show("product added !");
        }

    }

it works , but i'am still wondering why the first code doesnt work it keep adding that item to the list ? in other way how does the method .Contains()works ? what does it check to know if the item is or the list on not ? 

A:

The reason it doesn't find the object in the list is because it is a reference comparison, comparing the instances of the object, not the values. You can have two instances of your class with the same values in their properties, but if you compare them, they are not equal:
accessoire item1 = new accessoire();
item1.value1 = 1;
item1.value2 = "one";

accessoire item2 = new accessoire();
item2.value1 = 1;
item2.value2 = "one";

if(item1 == item2) MessageBox.Show("Same");
else MessageBox.Show("Different");

When you select the item from the list to compare with, you are pulling the specific instance, which does exist in the list.

