- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我创建了一个使用 SortedDictionary
来存储和操作数据的类。该类在多线程环境中实现时效果很好。现在,我想通过为内部 SortedDictionary
类编写一个包装类来使类线程安全。我想使用 Reader-Writer Locks 来实现这个,但是现在,我在编写包装类本身时遇到了问题。具体来说,我不确定如何为字典实现 Enumerator
。这是我现在的类(class)的完整代码。
public class ConcurrentSortedDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
{
#region Variables
SortedDictionary<TKey, TValue> _dict;
#endregion
#region Constructors
public ConcurrentSortedDictionary()
{
_dict = new SortedDictionary<TKey, TValue>();
}
public ConcurrentSortedDictionary(IComparer<TKey> comparer)
{
_dict = new SortedDictionary<TKey, TValue>(comparer);
}
public ConcurrentSortedDictionary(IDictionary<TKey, TValue> dictionary)
{
_dict = new SortedDictionary<TKey, TValue>(dictionary);
}
public ConcurrentSortedDictionary(IDictionary<TKey, TValue> dictionary, IComparer<TKey> comparer)
{
_dict = new SortedDictionary<TKey, TValue>(dictionary, comparer);
}
#endregion
#region Properties
public IComparer<TKey> Comparer
{
get
{
return _dict.Comparer;
}
}
public int Count
{
get
{
return _dict.Count;
}
}
public TValue this[TKey key]
{
get
{
return _dict[key];
}
set
{
_dict[key] = value;
}
}
public SortedDictionary<TKey, TValue>.KeyCollection Keys
{
get
{
return new SortedDictionary<TKey,TValue>.KeyCollection(_dict);
}
}
public SortedDictionary<TKey, TValue>.ValueCollection Values
{
get
{
return new SortedDictionary<TKey, TValue>.ValueCollection(_dict);
}
}
#endregion
#region Methods
public void Add(TKey key, TValue value)
{
_dict.Add(key, value);
}
public void Clear()
{
_dict.Clear();
}
public bool ContainsKey(TKey key)
{
return _dict.ContainsKey(key);
}
public bool ContainsValue(TValue value)
{
return _dict.ContainsValue(value);
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int index)
{
_dict.CopyTo(array, index);
}
public override bool Equals(Object obj)
{
return _dict.Equals(obj);
}
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _dict.GetEnumerator();
}
public override int GetHashCode()
{
return _dict.GetHashCode();
}
public bool Remove(TKey key)
{
return _dict.Remove(key);
}
public override string ToString()
{
return _dict.ToString();
}
public bool TryGetValue(TKey key, out TValue value)
{
return _dict.TryGetValue(key, out value);
}
#endregion
}
当我编译代码时,我收到错误消息:
'ConcurrentSortedDictionary' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'ConcurrentSortedDictionary.GetEnumerator()' cannot implement 'System.Collections.IEnumerable.GetEnumerator()' because it does not have the matching return type of 'System.Collections.IEnumerator'.
我在这里查看了几篇与此相关的帖子作为引用:
How do I implement IEnumerable in my Dictionary wrapper class that implements IEnumerable<Foo>? What's the best way of implementing a thread-safe Dictionary?
但我看不出我做错了什么。非常感谢任何帮助。
最佳答案
问题出在这里:
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _dict.GetEnumerator();
}
你需要:
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _dict.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _dict.GetEnumerator();
}
第二个非泛型 GetEnumerator()
是一个 explicit interface implementation并且需要作为对 C# 中存在泛型和泛型集合之前的日子的不幸倒退。
另请参阅:IEnumerable<T> provides two GetEnumerator methods - what is the difference between them? (特别是 Michael B's answer )。
但是如果您希望枚举与您的类的其余部分一起是线程安全的,您可能还需要编写自己的线程安全IEnumerator
类型来配合在您的类(class)中使用读者/作者锁!
关于c# - 线程安全的 SortedDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22308067/
在 C# 中,如何使用 LINQ 过滤 SortedDictionary,生成一个子集,该子集也是 SortedDictionary?例如。我想写 SortedDictionary source =
我有一个要求,我已经有一个现有的 SortedDictionary .现在我正在创建一个不同的 SortedDictionary并喜欢在第一个中添加它。怎么做? 最佳答案 只需将它传递给构造函数: v
我有 2 个 SortedDictionary“mainsd”&&“valuesd”。我正在尝试编程的条件如下: If the sum of Values of the 2 Keys above th
只是想知道:如果我有两个 SortedDictionary 对象,找出它们的内容是否相同的最快方法是什么?循环所有键并检查值听起来不像是最佳解决方案。仅检查 GetHashCode() 就足够了吗?
我有一个对象列表。这些对象有很多属性,包括价格和数量。我需要创建一个包含键“价格”和值“数量”的新字典。如果两个对象具有相同的价格,则生成的字典应将价格作为键,将两个对象的数量之和作为值。据我所知,我
我有一个对象列表。这些对象有很多属性,包括价格和数量。我需要创建一个包含键“价格”和值“数量”的新字典。如果两个对象具有相同的价格,则生成的字典应将价格作为键,将两个对象的数量之和作为值。据我所知,我
我正在使用一个 SortedDictionary,其中键是整数,值是字符串。 SortedDictionary dic = new SortedDictionary(); 现在假设我添加如下值 dic
我从来没有使用过 SortedDictionary,我只是好奇,当你向它添加值时,例如,在 for 循环中,是在添加值时自动对它们进行排序,还是在添加它们之后必须对它们进行排序. 最佳答案 这是自动完
所以基本上我有这样的事情: private SortedDictionary> example = new SortedDictionary>(new PriorityComparer()); pub
我在 Internet 上看到了很多关于此的引述,但没有官方文档?谁能告诉我在哪里可以获得这方面的信息? 最佳答案 这不应该记录在案,因为它是一个实现细节。 例如,SortedDictionary 的
我读了here与 SortedList 不同,SortedDictionary 不允许索引检索。那么如何在下面的代码片段中正确获取nameAddr["C"]呢? SortedDicti
这个问题在这里已经有了答案: Reverse Sorted Dictionary in .NET (5 个答案) 关闭 8 年前。 我有以下字典: SortedDictionary dictiona
我创建了一个使用 SortedDictionary 来存储和操作数据的类。该类在多线程环境中实现时效果很好。现在,我想通过为内部 SortedDictionary 类编写一个包装类来使类线程安全。我想
我必须尽快从 SortedDictionary 中删除第二个元素。字典 ( SortedDictionary> ) 最多可以包含 20'000 个元素。所以我想出了这个解决方案: try {
由于 SortedDictionary.ValueCollection 的 Enumerator 行为与其他枚举器不同,我们最近遇到了一个错误。我已经设法将问题缩小到以下(无意义的)示例: publi
我正在研究我的一个项目的代码优化,我想知道如何为 SortedDictionary 设置初始容量。我知道我可以在构造函数中很容易地对列表和字典执行此操作。但是我该如何对 SortedDictionar
有很多很多线程讨论如何从字典获取“第一个”项目,并且有各种各样的答案来解释为什么这样的事情实际上并不是一件好事这个想法是因为没有内部排序。但我的是一个 SortedDictionary,所以这些参数不
例如,假设我有大量关于数据库中大量日期的一组餐厅的数据,我需要分析/输出给用户。 因此,在我的代码中,我有一个自定义类,用于保存每个日期每个餐厅的数据 - 例如: Public Class DateD
C# 中 SortedDictionary.Count 的成本是多少?它是检索一些整数,还是遍历树? 最佳答案 它只是从一个字段返回一个值,因此成本应该可以忽略不计。 关于c# - SortedDic
我有一个 SortedDictionary 的集合: SortedDictionary _variableValues = new SortedDictionary(); 键值对如下所示: ThkPo
我是一名优秀的程序员,十分优秀!