小编rod*_*ins的帖子

使用 Linq 获取这些值后访问 ConcurrentDictionary 值是否线程安全

我有一个像这样的 ConcurrentDictionary:

ConcurrentDictionary<int, Dto> concurrentDictionary = new ConcurrentDictionary<int, Dto>();
Run Code Online (Sandbox Code Playgroud)

这是一个可读可写的字典,可以被多个线程使用。我可以以线程安全的方式管理可写端。当我需要访问字典中的 Dto 时,我总是使用 Linq select 方法从中获取 Dto。

IEnumerable<Dto> dtos = concurrentDictionary.Select(p => p.Value);
Run Code Online (Sandbox Code Playgroud)

我知道,并发字典上的 Linq 方法对于可读和可写的并发字典来说是无锁和线程安全的。在我使用 Linq 访问 Dto 之后,在这些 Dto 上使用一些读取函数是否是线程安全的?我的意思是在 foreach 循环中访问这些 Dto 或从中创建新列表并对这些 Dto 的属性进行过滤或排序?

ConcurrentDictionary<int, Dto> concurrentDictionary = new ConcurrentDictionary<int, Dto>();
for (int i = 0; i < 10000; i++)
{
    concurrentDictionary.TryAdd(i, new Dto()
    { Id = i, Name = RandomString(35), Type = "new", IsActive = true} );
}

IEnumerable<Dto> dtos = concurrentDictionary.Select(p => p.Value);

ArrayList arrayList …
Run Code Online (Sandbox Code Playgroud)

c# linq multithreading thread-safety concurrentdictionary

4
推荐指数
1
解决办法
2252
查看次数

什么是C#中的Java的float.floatToRawIntBits等效方法?

什么是Java的C#中的float.floatToRawIntBits方法实现?

c# java

3
推荐指数
1
解决办法
901
查看次数