仅将唯一项添加到列表

Oli*_*Oli 70 .net c# c#-4.0

我正在通过网络宣布自己将远程设备添加到列表中.我只想将设备添加到列表中,如果之前没有添加过的话.

公告是通过异步套接字侦听器进行的,因此添加设备的代码可以在多个线程上运行.我不确定我做错了什么但没有我尝试的东西我最终得到重复.这是我现在拥有的......

lock (_remoteDevicesLock)
{
    RemoteDevice rDevice = (from d in _remoteDevices
                            where d.UUID.Trim().Equals(notifyMessage.UUID.Trim(), StringComparison.OrdinalIgnoreCase)
                            select d).FirstOrDefault();
     if (rDevice != null)
     {
         //Update Device.....
     }
     else
     {
         //Create A New Remote Device
         rDevice = new RemoteDevice(notifyMessage.UUID);
         _remoteDevices.Add(rDevice);
     }
}
Run Code Online (Sandbox Code Playgroud)

Aus*_*nen 132

如果您的要求没有重复,那么您应该使用HashSet.

当项目已经存在时,HashSet.Add将返回false(如果这对您很重要).

您可以使用@pstrjds链接到下面(或此处)的构造函数来定义相等运算符,或者您需要在RemoteDevice(GetHashCode&Equals)中实现相等方法.

  • 这里一个重要的注意事项是HashSet不保证尊重插入顺序.因此,如果顺序很重要(项目需要以与放入它们相同的顺序出现在列表中,就像`List <T>`所发生的那样),那么HashSet不能正常工作. (9认同)
  • 即将添加这个答案.您可以使用此重载来定义比较 - http://msdn.microsoft.com/en-us/library/bb359100(v=vs.100).aspx (3认同)
  • @Oli 你需要保持锁定,但操作会*快得多*,所以他们不会互相等待。不幸的是,没有 `ConcurrentSet` 类。然而,有一个 `ConcurrentDictionary` 类,所以你可以使用它,将你的值存储为键,并将 `null` 存储在值中。 (2认同)

小智 18

//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);
Run Code Online (Sandbox Code Playgroud)


小智 10

就像接受的答案说HashSet没有订单一样.如果订单很重要,您可以继续使用列表,并在添加之前检查它是否包含该项目.

if (_remoteDevices.Contains(rDevice))
    _remoteDevices.Add(rDevice);
Run Code Online (Sandbox Code Playgroud)

在自定义类/对象上执行List.Contains()需要IEquatable<T>在自定义类上实现或覆盖自定义类Equals.同样GetHashCode在课堂上实施也是个好主意.这是根据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;
    }
}
Run Code Online (Sandbox Code Playgroud)