41

我只是将此代码用作示例。假设我有以下 Person 类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace dictionaryDisplay
{
class Person
{
    public string FirstName { get; private set;}
    public string LastName { get; private set; }

    public Person(string firstName, string lastName)
    {
        this.FirstName = firstName;
        this.LastName = lastName;

    }

    public override string ToString()
    {
        return this.FirstName + " " + this.LastName;
    }
}

}

主程序

static void Main(string[] args)
    {
        ConcurrentDictionary<int, Person> personColl = new ConcurrentDictionary<int,   Person>();

        personColl.TryAdd(0, new Person("Dave","Howells"));
        personColl.TryAdd(1, new Person("Jastinder","Toor"));

        Person outPerson = null;
        personColl.TryRemove(0, out outPerson);


        //Is this safe to do?
        foreach (var display in personColl)
        {
            Console.WriteLine(display.Value);
        }





    }
  1. 这是迭代并发字典的安全方法吗?如果不是,那么安全的方法是什么?

  2. 假设我想从字典中删除一个 Person 对象。我使用 tryRemove 方法,但我该如何处理 outPerson 对象?从字典中删除的 Person 存储在其中。我该如何处理 outPerson 对象来完全清除它?

4

2 回答 2

63

这是迭代并发字典的安全方法吗?如果不是,那么安全的方法是什么?

是的,它是安全的,因为它不会抛出异常。如果在开始迭代后添加或删除元素,它们可能包含在迭代中,也可能不包含在迭代中。从GetEnumerator文档中:

从字典返回的枚举器可以安全地与字典的读取和写入同时使用,但它并不代表字典的即时快照。通过枚举器公开的内容可能包含调用 GetEnumerator 后对字典所做的修改。

下一个:

我使用 tryRemove 方法,但我该如何处理 outPerson 对象?

无论你想要什么,包括什么。您可以将字典转换为IDictionary<TKey, TValue>并调用Remove,或者TryRemove之后使用并忽略该变量:

Person ignored;
dictionary.TryRemove(key, out ignored);

或者您可以使用 C# 7.0 功能 Discards

dictionary.TryRemove(key, out _);

没有“完全清除[对象]”的概念-如果您没有对它的任何引用,它将被垃圾收集。但无论哪种方式,它不再在字典中(至少通过那个键)。如果您不在ignored代码中的其他任何地方使用变量(上面),它不会阻止对象被垃圾收集。

于 2013-07-21T20:14:26.760 回答
2

看看这篇文章。

TryRemove() was added to attempt atomic, safe removes.

    To safely attempt to remove a value we need to see if the key exists first, this checks for existence and removes under an atomic lock.

由于 TryRemove 将从集合中删除项目,因此您可能需要键的值。

用 foreach 迭代它是安全的。你不会得到例外。

于 2013-07-21T20:14:52.043 回答