.Contains() on a list of custom class objects

前端 未结 7 2169
天命终不由人
天命终不由人 2020-11-27 02:38

I\'m trying to use the .Contains() function on a list of custom objects

This is the list:

List CartProducts = new Lis         


        
相关标签:
7条回答
  • 2020-11-27 03:38

    It checks to see whether the specific object is contained in the list.

    You might be better using the Find method on the list.

    Here's an example

    List<CartProduct> lst = new List<CartProduct>();
    
    CartProduct objBeer;
    objBeer = lst.Find(x => (x.Name == "Beer"));
    

    Hope that helps

    You should also look at LinQ - overkill for this perhaps, but a useful tool nonetheless...

    0 讨论(0)
提交回复
热议问题