Only Add Unique Item To List

后端 未结 3 1384
攒了一身酷
攒了一身酷 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:37

    Just like the accepted answer says a HashSet doesn't have an order. If order is important you can continue to use a List and check if it contains the item before you add it.

    if (_remoteDevices.Contains(rDevice))
        _remoteDevices.Add(rDevice);
    

    Performing List.Contains() on a custom class/object requires implementing IEquatable<T> on the custom class or overriding the Equals. It's a good idea to also implement GetHashCode in the class as well. This is per the documentation at https://msdn.microsoft.com/en-us/library/ms224763.aspx

    public class RemoteDevice: IEquatable<RemoteDevice>
    {
        private readonly int id;
        public RemoteDevice(int uuid)
        {
            id = id
        }
        public int GetId
        {
            get { return id; }
        }
    
        // ...
    
        public bool Equals(RemoteDevice other)
        {
            if (this.GetId == other.GetId)
                return true;
            else
                return false;
        }
        public override int GetHashCode()
        {
            return id;
        }
    }
    
    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2021-02-01 00:49
    //HashSet allows only the unique values to the list
    HashSet<int> uniqueList = new HashSet<int>();
    
    var a = uniqueList.Add(1);
    var b = uniqueList.Add(2);
    var c = uniqueList.Add(3);
    var d = uniqueList.Add(2); // should not be added to the list but will not crash the app
    
    //Dictionary allows only the unique Keys to the list, Values can be repeated
    Dictionary<int, string> dict = new Dictionary<int, string>();
    
    dict.Add(1,"Happy");
    dict.Add(2, "Smile");
    dict.Add(3, "Happy");
    dict.Add(2, "Sad"); // should be failed // Run time error "An item with the same key has already been added." App will crash
    
    //Dictionary allows only the unique Keys to the list, Values can be repeated
    Dictionary<string, int> dictRev = new Dictionary<string, int>();
    
    dictRev.Add("Happy", 1);
    dictRev.Add("Smile", 2);
    dictRev.Add("Happy", 3); // should be failed // Run time error "An item with the same key has already been added." App will crash
    dictRev.Add("Sad", 2);
    
    0 讨论(0)
提交回复
热议问题