C# 将字典作为ICollection返回
我有一个可以实现各种集合和数据类型的接口,它可以很好地处理某些集合,但是字典给了我一些问题,我猜是因为字典有点不同,并且有键值对C# 将字典作为ICollection返回,c#,generics,collections,dictionary,icollection,C#,Generics,Collections,Dictionary,Icollection,我有一个可以实现各种集合和数据类型的接口,它可以很好地处理某些集合,但是字典给了我一些问题,我猜是因为字典有点不同,并且有键值对 public interface IStructure { void InsertRun<T> (T item); ICollection RetrieveSortedListRun<T>(); T RetrieveItemRun<T>(T item); } class DictionaryRun : IS
public interface IStructure
{
void InsertRun<T> (T item);
ICollection RetrieveSortedListRun<T>();
T RetrieveItemRun<T>(T item);
}
class DictionaryRun : IStructure
{
IDictionary<int, object> dictionary;
public DictionaryRun()
{
dictionary = new Dictionary<int, object>();
}
public void InsertRun<T>(T item)
{
dictionary.Add(dictionary.Count + 1, item);
}
public ICollection RetrieveSortedListRun<T>()
{
return dictionary;
}
public T RetrieveItemRun<T>(T item)
{
return item;
}
}
公共接口结构
{
无效插入运行(T项);
ICollection RetrieveSortedListRun();
T RetrieveItemRun(T项);
}
类字典Run:IStructure
{
词典;
公共字典run()
{
字典=新字典();
}
公共void InsertRun(T项)
{
dictionary.Add(dictionary.Count+1,item);
}
公共ICollection RetrieveSortedListRun()检索
{
返回字典;
}
公共T RetrieveItemRun(T项)
{
退货项目;
}
}
是,但Dictionary有一种将自身转换为列表的方法,该方法实现了ICollection:
public ICollection RetrieveSortedListRun<T>()
{
return dictionary.ToList();
}
public ICollection RetrieveSortedListRun()
{
返回dictionary.ToList();
}
这将返回一个
KeyValuePair
s的集合。是,但Dictionary有一个将自身转换为列表的方法,该方法实现了ICollection:
public ICollection RetrieveSortedListRun<T>()
{
return dictionary.ToList();
}
public ICollection RetrieveSortedListRun()
{
返回dictionary.ToList();
}
这将返回KeyValuePair
s的集合。IDictionary
不实现ICollection
,而是实现ICollection
按原样,如果您将词典更改为IDictionary
,您的代码将被编译
然而,在我看来,您的界面和对象的总体设计可以重新设计
public interface IStructure<T>
{
void InsertRun(T item);
ICollection<T> RetrieveSortedListRun();
T RetrieveItemRun(T item);
}
class DictionaryRun<T> : IStructure<T>
{
IDictionary<int, T> dictionary;
public DictionaryRun()
{
dictionary = new Dictionary<int, T>();
}
public void InsertRun(T item)
{
dictionary.Add(dictionary.Count + 1, item);
}
public ICollection<T> RetrieveSortedListRun()
{
return dictionary.Values;
}
public T RetrieveItemRun(T item)
{
return item;
}
}
公共接口结构
{
无效插入运行(T项);
ICollection RetrieveSortedListRun();
T RetrieveItemRun(T项);
}
类字典Run:IStructure
{
词典;
公共字典run()
{
字典=新字典();
}
公共void InsertRun(T项)
{
dictionary.Add(dictionary.Count+1,item);
}
公共ICollection RetrieveSortedListRun()检索
{
返回字典值;
}
公共T RetrieveItemRun(T项)
{
退货项目;
}
}
IDictionary
不实现ICollection
,它实现ICollection
按原样,如果您将词典更改为IDictionary
,您的代码将被编译
然而,在我看来,您的界面和对象的总体设计可以重新设计
public interface IStructure<T>
{
void InsertRun(T item);
ICollection<T> RetrieveSortedListRun();
T RetrieveItemRun(T item);
}
class DictionaryRun<T> : IStructure<T>
{
IDictionary<int, T> dictionary;
public DictionaryRun()
{
dictionary = new Dictionary<int, T>();
}
public void InsertRun(T item)
{
dictionary.Add(dictionary.Count + 1, item);
}
public ICollection<T> RetrieveSortedListRun()
{
return dictionary.Values;
}
public T RetrieveItemRun(T item)
{
return item;
}
}
公共接口结构
{
无效插入运行(T项);
ICollection RetrieveSortedListRun();
T RetrieveItemRun(T项);
}
类字典Run:IStructure
{
词典;
公共字典run()
{
字典=新字典();
}
公共void InsertRun(T项)
{
dictionary.Add(dictionary.Count+1,item);
}
公共ICollection RetrieveSortedListRun()检索
{
返回字典值;
}
公共T RetrieveItemRun(T项)
{
退货项目;
}
}
ADictionary
实现ICollection
而不是ICollection
。似乎您从未使用过该键,那么为什么不使用HashSet
<代码>哈希集实现ICollection
更新 你的代码行
dictionary.Add(dictionary.Count + 1, item);
让我想想,你所需要的只是一个有序的列表。与排序列表(根据键对列表进行排序)不同,排序列表保留原始的插入顺序。因此,最好使用列表
字典
实现ICollection
而不是ICollection
。似乎您从未使用过该键,那么为什么不使用HashSet
<代码>哈希集实现ICollection
更新 你的代码行
dictionary.Add(dictionary.Count + 1, item);
让我想想,你所需要的只是一个有序的列表。与排序列表(根据键对列表进行排序)不同,排序列表保留原始的插入顺序。因此,您最好使用
列表
好吧……那么您如何使用ICollection
以及目前有哪些问题阻止它工作?好吧……那么您如何使用ICollection
以及目前有哪些问题阻止它工作?我同意,如果你从未使用过字典中的键,为什么还要使用字典呢。如果你想允许重复使用列表,如果你不想允许重复使用hashset,我同意,如果你从来没有使用字典中的键,为什么还要使用字典呢。如果您想允许重复使用列表,如果不想让重复使用HashSetSeems工作得很好,我在实现其他集合(如数组列表和hashtable)并尝试在RetrieveSortedList方法中返回它们时遇到问题。arraylist和hashtable是否只实现ICollection而不实现ICollection?本程序的目的是测量各种数据结构的效率。我应该了解一下如何将不同的集合和接口一起使用。@KosmoArrayList
和Hashtable
只实现了ICollection
,有一些键入选项,比如List
和HashSet
,你可以看看哪些实现了ICollection
,看起来效果不错,我在实现其他集合(如数组列表和哈希表)并尝试在RetrieveSortedList方法中返回它们时遇到问题。arraylist和hashtable是否只实现ICollection而不实现ICollection?本程序的目的是测量各种数据结构的效率。我应该读一读如何一起使用不同的集合和接口。@KosmoArrayList
和Hashtable
只实现ICollection
,有一些类型化选项,如List
和HashSet
,您可以查看哪些实现了ICollection
字典不