Czy jest to właściwy sposób iteracji nad Concurrentdictionary w C #

Używam tego kodu tylko na przykład. Załóżmy, że mam następującą klasę 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;
    }
}

}

Główny program

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);
        }





    }

czy jest to bezpieczny sposób iteracji słownika współbieżnego? Jeśli nie, jaki jest bezpieczny sposób na to?

Powiedzmy, że chcę usunąć obiekt Person ze słownika. Używam metody tryRemove, ale co mam zrobić z obiektem outPerson? usunięta osoba ze słownika jest w niej przechowywana. Co mam zrobić z obiektem outPerson, aby całkowicie go wyczyścić?

questionAnswers(2)

yourAnswerToTheQuestion