这是遍历Concurrentdictionary在C#中的正确方法 [英] Is this the proper way to iterate over Concurrentdictionary in C#

查看:6569
本文介绍了这是遍历Concurrentdictionary在C#中的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只使用一个例子此代码。 。假设我有以下Person类

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;

命名空间dictionaryDisplay
{
类Person
{
公共字符串名字{获得;私人集;}
公共字符串名字{获得;私人集; }

公众人物(字符串名字,字符串的lastName)
{
this.FirstName =名字;
this.LastName = lastName的;

}

公共重写字符串的ToString()
{
返回this.FirstName ++ this.LastName;
}
}



}



主程序

 静态无效的主要(字串[] args)
{
ConcurrentDictionary< ; INT,人> personColl =新ConcurrentDictionary< INT,人>();

personColl.TryAdd(0,新的Person(戴夫,威尔斯));
personColl.TryAdd(1,新的人(Jastinder,托尔));

的人outPerson = NULL;
personColl.TryRemove(0,出outPerson);


//这是安全的呢?
的foreach(在personColl VAR显示屏)
{
Console.WriteLine(display.Value);
}





}




  1. 这是遍历并发字典的安全的方式?如果没有,什么是做它的安全的方式?


  2. 比方说,我想从词典中删除Person对象。我用的是tryRemove方法,但我该怎么做的outPerson对象吗?从字典取出的人被存储在其中。我怎么用outPerson对象做到彻底清除它?



解决方案

这是安全的方式遍历并发字典?如果没有,什么是做它的安全的方式?




是的,它是安全的,因为它不会抛出异常。如果元素的添加或者在开始迭代后取出,它们可以或可以不被包括在迭代。从 的GetEnumerator 文档:




普查员从字典中返回的安全与读取和写入字典同时使用,但它并不代表一刻即时字典的快照。 ,内容通过枚举可以包含的GetEnumerator被称为后字典的修改暴露




下一步:




我用的是tryRemove方法,但我该怎么做的outPerson对象?




无论你想用它,包括什么都没有。你可以只投的字典的IDictionary< TKEY的,TValue> 并调用删除,或只是使用 TryRemove 而忽视事后变量:忽略

 人; 
dictionary.TryRemove(键,出忽略);

有没有的清算[对象]彻底的概念 - 如果你没有得到任何引用它,它就会被垃圾收集。但无论哪种方式,它不是在字典中的任何更多的(通过该键至少)。如果不使用变量(忽略以上)在代码中其他地方,它不会停止被垃圾收集的对象。


I'm only using this code for an example. Assume I have the following Person class.

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

}

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





    }

  1. is this the safe way of iterating over a concurrent dictionary? If not, what is the safe way for doing it?

  2. Lets say that I want to remove a Person object from the dictionary. I use the tryRemove method, but what do I do with the outPerson object? the removed Person from the dictionary is stored in it. What do I do with the outPerson object to clear it completely?

解决方案

is this the safe way of iterating over a concurrent dictionary? If not, what is the safe way for doing it?

Yes, it's safe in that it won't throw an exception. If elements are added or removed after you start iterating, they may or may not be included in the iteration. From the GetEnumerator documentation:

The enumerator returned from the dictionary is safe to use concurrently with reads and writes to the dictionary, however it does not represent a moment-in-time snapshot of the dictionary. The contents exposed through the enumerator may contain modifications made to the dictionary after GetEnumerator was called.

Next:

I use the tryRemove method, but what do I do with the outPerson object?

Whatever you want with it, including nothing. You could just cast the dictionary to IDictionary<TKey, TValue> and call Remove, or just use TryRemove and ignore the variable afterwards:

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

There's no concept of "clearing [the object] completely" - if you haven't got any references to it, it will be garbage collected. But either way, it's not in the dictionary any more (at least via that key). If you don't use the variable (ignored above) anywhere else in the code, it won't stop the object from being garbage collected.

这篇关于这是遍历Concurrentdictionary在C#中的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆