C# 直接从 Dictionary<> 获取 KeyValuePair<>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1619090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting a KeyValuePair<> directly from a Dictionary<>
提问by user200783
I have System.Collections.Generic.Dictionary<A, B> dict
where A and B are classes, and an instance A a
(where dict.ContainsKey(a)
is true).
我有System.Collections.Generic.Dictionary<A, B> dict
A 和 B 是类和一个实例A a
(其中dict.ContainsKey(a)
为真)。
Is it possible to get the KeyValuePair containing a
directly from the Dictionary?
Or do I need to create a new KeyValuePair: new KeyValuePair<A, B>(a, dict[a])
?
是否可以a
直接从字典中获取包含的 KeyValuePair ?
还是我需要创建一个新的 KeyValuePair: new KeyValuePair<A, B>(a, dict[a])
?
采纳答案by Jon Skeet
You need to create a new KeyValuePair
1- but bear in mind that KVP is a value type (a struct) anyway, so it's not like you're introducing a new inefficiency by doing this. Any method returning a KVP would be creating a copy anyway - you're just creating the instance directly.
您需要创建一个新的KeyValuePair
1- 但请记住,无论如何 KVP 都是值类型(结构),因此您不会通过这样做引入新的低效率。任何返回 KVP 的方法无论如何都会创建一个副本 - 您只是直接创建实例。
You could always add an extension method to IDictionary<TKey, TValue>
if you wanted:
如果需要,您可以随时添加扩展方法IDictionary<TKey, TValue>
:
public static KeyValuePair<TKey, TValue> GetEntry<TKey, TValue>
(this IDictionary<TKey, TValue> dictionary,
TKey key)
{
return new KeyValuePair<TKey, TValue>(key, dictionary[key]);
}
As noted in comments, it's entirely possible that the key which is stored in the dictionary is not the sameas the one provided, just semantically equal - by some semantics which could be customized by an IEqualityComparer
(as with a case-insensitive dictionary, for example.) In that case, the code above would not return the actual entry in the dictionary, but an entry with the key you provided to look up. Unfortunately there's no efficient way of finding the original key - you'd have to iterate over the dictionary :(
正如评论中所指出的,存储在字典中的键完全有可能与提供的键不同,只是在语义上相等 - 通过一些可以由 自定义的语义IEqualityComparer
(例如不区分大小写的字典,例如.) 在这种情况下,上面的代码将不会返回字典中的实际条目,而是返回一个带有您提供的用于查找的键的条目。不幸的是,没有找到原始密钥的有效方法-您必须遍历字典:(
1I was aware that you could iterate over the dictionary entries and find the appropriate entry that way, but I can see no reason why you'd ever want to do so when you've got a perfectly good indexer which is O(1) instead of O(N).
1我知道您可以遍历字典条目并以这种方式找到合适的条目,但是当您拥有 O(1) 的完美索引器时,我看不出您为什么要这样做而不是 O(N)。
回答by Oliver Hanappi
As Dictionary<TKey, TValue>
implements IEnumerable<KeyValuePair<TKey, TValue>>
, you could use linq:
作为Dictionary<TKey, TValue>
实现IEnumerable<KeyValuePair<TKey, TValue>>
,您可以使用 linq:
var pair = _dictionary.SingleOrDefault(p => p.Key == myKey);
回答by johv
We can't get to "IPHone"
this way:
我们不能"IPHone"
这样:
var dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "IPHone", "TCP/IP honing tools" }
};
Console.WriteLine(dict["iPhone"]); // "TCP/IP honing tools"
Console.WriteLine( ??? ); // "IPHone"
There's seems to be no O(1) solution with the current API, but looping through all entries works:
当前 API 似乎没有 O(1) 解决方案,但循环遍历所有条目有效:
var keyValue = dict.First(p => dict.Comparer.Equals(p.Key, "iPhone"));
Console.WriteLine(keyValue.Key); // "IPHone"
Console.WriteLine(keyValue.Value); // "TCP/IP honing tools"
Or as an extension for the lazy:
或者作为懒人的扩展:
[Pure]
public static KeyValuePair<TKey, TValue> GetEntry<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key)
{
var comparer = dictionary.Comparer;
return dictionary.FirstOrDefault(p => comparer.Equals(p.Key, key));
}
回答by Silvan Hofer
For me myDict.AsEnumerable
does it...
对我myDict.AsEnumerable
来说...