Only Add Unique Item To List

后端 未结 3 1387
攒了一身酷
攒了一身酷 2021-02-01 00:28

I\'m adding remote devices to a list as they announce themselves across the network. I only want to add the device to the list if it hasn\'t previously been added.

The

3条回答
  •  不知归路
    2021-02-01 00:47

    If your requirements are to have no duplicates, you should be using a HashSet.

    HashSet.Add will return false when the item already exists (if that even matters to you).

    You can use the constructor that @pstrjds links to below (or here) to define the equality operator or you'll need to implement the equality methods in RemoteDevice (GetHashCode & Equals).

提交回复
热议问题