how to check if object already exists in a list

前端 未结 9 1823
梦谈多话
梦谈多话 2020-11-28 05:41

I have a list

  List myList

and I am adding items to a list and I want to check if that object is already in the list.

9条回答
  •  天命终不由人
    2020-11-28 06:11

    Simple but it works

    MyList.Remove(nextObject)
    MyList.Add(nextObject)
    

    or

     if (!MyList.Contains(nextObject))
        MyList.Add(nextObject);
    

提交回复
热议问题