C# 如何在IDictionary中存储相同的密钥?
我用的是C4.0。 我想使用IDictionary存储字符串、字符串对。 如下图所示:C# 如何在IDictionary中存储相同的密钥?,c#,c#-4.0,idictionary,C#,C# 4.0,Idictionary,我用的是C4.0。 我想使用IDictionary存储字符串、字符串对。 如下图所示: Dictionary<string, string> _tempDicData = new Dictionary<string, string>(); _tempDicData.Add("Hello", "xyz"); _tempDicData.Add("Hello", "aaa"); _tempDicData.Add("Hello", "qwert
Dictionary<string, string> _tempDicData = new Dictionary<string, string>();
_tempDicData.Add("Hello", "xyz");
_tempDicData.Add("Hello", "aaa");
_tempDicData.Add("Hello", "qwert");
_tempDicData.Add("Hello", "foo");
_tempDicData.Add("Hello", "pqr");
_tempDicData.Add("Hello", "abc");
那么,如何在IDictionary中存储相同的密钥呢?您不能。字典只能为每个键保存一项。
字典可能不是您要查找的数据结构。您不能。字典只能为每个键保存一项。
字典可能不是您要查找的数据结构。您不能多次将同一项添加到IDictionary,这就是字典的全部意义,一个关联容器。但您可以这样替换现有的:
_tempDicData.Add("Hello", "xyz");
_tempDicData["Hello"] = "aaa";
如果每个键需要多个项,则可以创建列表字典:
IDictionary<string,IList<string>> _tempDicData =
new Dictionary<string,IList<string>>();
IList<string> lst = new List<string>();
lst.Add("xyz");
lst.Add("aaa");
_tempDicData.Add("Hello", lst);
当您不确定某个键的列表是否存在时,可以使用此模式添加新项:
IList<string> lst;
if (!_tempDicData.TryGetValue("Hello", out lst)) {
_tempDicData.Add("Hello", lst);
}
lst.Add("xyz");
lst.Add("aaa");
您不能多次将同一项添加到IDictionary中,这是字典(关联容器)的全部要点。但您可以这样替换现有的:
_tempDicData.Add("Hello", "xyz");
_tempDicData["Hello"] = "aaa";
如果每个键需要多个项,则可以创建列表字典:
IDictionary<string,IList<string>> _tempDicData =
new Dictionary<string,IList<string>>();
IList<string> lst = new List<string>();
lst.Add("xyz");
lst.Add("aaa");
_tempDicData.Add("Hello", lst);
当您不确定某个键的列表是否存在时,可以使用此模式添加新项:
IList<string> lst;
if (!_tempDicData.TryGetValue("Hello", out lst)) {
_tempDicData.Add("Hello", lst);
}
lst.Add("xyz");
lst.Add("aaa");
不可能,为此目的使用字段键值创建自己的类不可能,为此目的使用字段键值创建自己的类创建字典 创建字典 你不能 您可以使用字典,或者,如果您想要只读解决方案,可以使用ToLookup扩展方法
var lp = new[]
{
Tuple.Create("Hello", "xyz"),
Tuple.Create("Hello", "aaa"),
Tuple.Create("Hello", "qwert"),
Tuple.Create("Hello", "foo"),
Tuple.Create("Hello", "pqr"),
Tuple.Create("Hello", "abc"),
}.ToLookup(p => p.Item1, p => p.Item2);
foreach (var key in lp)
{
foreach (var value in key)
{
Console.WriteLine("{0}: {1}", key.Key, value);
}
}
你不能
您可以使用字典,或者,如果您想要只读解决方案,可以使用ToLookup扩展方法
var lp = new[]
{
Tuple.Create("Hello", "xyz"),
Tuple.Create("Hello", "aaa"),
Tuple.Create("Hello", "qwert"),
Tuple.Create("Hello", "foo"),
Tuple.Create("Hello", "pqr"),
Tuple.Create("Hello", "abc"),
}.ToLookup(p => p.Item1, p => p.Item2);
foreach (var key in lp)
{
foreach (var value in key)
{
Console.WriteLine("{0}: {1}", key.Key, value);
}
}
键必须是唯一的才能搜索值。键必须是唯一的才能搜索值。我认为您必须创建自己的类,可以在其中多次添加相同的键。
字典是错误的方法。我认为您必须创建自己的类,在其中可以多次添加相同的键。
字典是错误的方法。字典中不能有两个键相同的项。但是,您可以有一个项目本身就是一个集合:
Dictionary<string, List<string>> _tempDicData = new Dictionary<string, List<string>>
{
{ "Hello", new List<string> { "xyz", "aa", "foo" },
};
如果这样做,访问项目时必须小心,因为基础列表可能已经创建,也可能尚未创建:
// WRONG: _tempDicData["Hello"] might not exist!
_tempDicData["Hello"].Add("bar");
// CORRECT:
if (!_tempDicData.ContainsKey("Hello")) {
_tempDicData["Hello"] = new List<string>();
}
_tempDicData["Hello"].Add("bar");
字典中不能有两个键相同的项。但是,您可以有一个项目本身就是一个集合:
Dictionary<string, List<string>> _tempDicData = new Dictionary<string, List<string>>
{
{ "Hello", new List<string> { "xyz", "aa", "foo" },
};
如果这样做,访问项目时必须小心,因为基础列表可能已经创建,也可能尚未创建:
// WRONG: _tempDicData["Hello"] might not exist!
_tempDicData["Hello"].Add("bar");
// CORRECT:
if (!_tempDicData.ContainsKey("Hello")) {
_tempDicData["Hello"] = new List<string>();
}
_tempDicData["Hello"].Add("bar");
你不能用IDictionary做这件事。尝试使用NameValueCollection 用IDictionary是做不到的。尝试使用NameValueCollection 为什么你会想这么做。 字典的概念是按键分类,如果出现多次,那就不是关键
您可以创建>的字典,但最好重新规划您的需求。为什么您要这样做。 字典的概念是按键分类,如果出现多次,那就不是关键
您可以创建>的字典,但最好重新规划您的需要。可以添加重复键的类可能如下所示:
class TDictionary<K, V>
{
struct KeyValuePair
{
public K Key;
public V Value;
}
private readonly List<KeyValuePair> fList = new List<KeyValuePair>();
public void Add(K key, V value)
{
fList.Add(new KeyValuePair { Key = key, Value = value });
}
public List<V> this[K key]
{
get { return (from pair in fList where pair.Key.Equals(key) select pair.Value).ToList(); }
}
public List<K> Keys
{
get { return fList.Select(pair => pair.Key).ToList(); }
}
public List<V> Values
{
get { return fList.Select(pair => pair.Value).ToList(); }
}
}
可以在其中添加重复密钥的类可能如下所示:
class TDictionary<K, V>
{
struct KeyValuePair
{
public K Key;
public V Value;
}
private readonly List<KeyValuePair> fList = new List<KeyValuePair>();
public void Add(K key, V value)
{
fList.Add(new KeyValuePair { Key = key, Value = value });
}
public List<V> this[K key]
{
get { return (from pair in fList where pair.Key.Equals(key) select pair.Value).ToList(); }
}
public List<K> Keys
{
get { return fList.Select(pair => pair.Key).ToList(); }
}
public List<V> Values
{
get { return fList.Select(pair => pair.Value).ToList(); }
}
}
你更大的目标是什么?你希望实现什么?这个问题的核心与非常相似,它的答案可能会帮助你解决问题。如果你想在同一个键中添加多个值,为什么不使用string[]作为value而不是string呢?你更广泛的目标是什么?你希望实现什么?这个问题的核心与非常相似,它的答案可能会帮助你解决问题。如果你想向同一个键添加多个值,为什么不使用string[]作为value而不是stringwhich类型的类?你能给我一个提示吗?公共类MyType{public string key{get;set;}公共字符串值{get;set;}哪种类型的类?你能给我一个提示吗?公共类MyType{public string Key{get;set;}公共字符串值{get;set;}