C# 从 Dictionary<string, string> 获取第一个键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1820506/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:06:45  来源:igfitidea点击:

Get first key from Dictionary<string, string>

c#

提问by Michel

I'm using a System.Collections.Generic.Dictionary<string, string>.

我正在使用一个System.Collections.Generic.Dictionary<string, string>.

I want to return the first key from this dictionary. I tried dic.Keys[0]but the only thing I have on the Keysproperty (which is a KeyCollectionobject) is an enumerator.

我想从这本字典中返回第一个键。我试过了,dic.Keys[0]但我在Keys属性(它是一个KeyCollection对象)上唯一的东西是一个枚举器。

Do I have to enumerate through all the keys to get the first one?

我是否必须枚举所有键才能获得第一个键?

采纳答案by Jon Skeet

Assuming you're using .NET 3.5:

假设您使用的是 .NET 3.5:

dic.Keys.First();

Note that there's no guaranteed order in which key/value pairs will be iterated over in a dictionary. You may well find that in lotsof cases you get out the first key that you put into the dictionaries - but you absolutely must notrely on that. As Skirwan says, there isn't really a notion of "first". Essentially the above will give you "a key from the dictionary" - that's about all that's guaranteed.

请注意,在字典中迭代键/值对的顺序没有保证。您很可能会发现,在很多情况下,您会取出放入词典中的第一个键 - 但您绝对不能依赖它。正如 Skirwan 所说,实际上并没有“第一”的概念。本质上,上面的内容会给你“字典中的一个键”——这就是保证的全部内容。

(If you don't know whether the dictionary is empty or not, you could use FirstOrDefault.)

(如果你不知道字典是否为空,你可以使用FirstOrDefault.)

回答by Thorsten Dittmar

Seems a bit over the top, but how about

似乎有点过头了,但是怎么样

foreach (<Type> k in dic.Keys)
    return k;

回答by Skirwan

The idea of 'first' doesn't really apply to a dictionary, as there's no ordering on the entries; any insert or remove is allowed to radically change the order of the keys.

“第一”的概念并不真正适用于字典,因为条目没有排序;任何插入或删除都可以从根本上改变键的顺序。

回答by Paul Ruane

Firstly, no, you do not have to enumerate all of the entries — just enumerate the first item:

首先,不,您不必枚举所有条目 - 只需枚举第一项:

IEnumerator enumerator = dictionary.Keys.GetEnumerator();
enumerator.MoveNext();
object first = enumerator.Current;

Ideally one would check that the MoveNext() actually succeeded (it returns a bool) otherwise the call to Current may throw.

理想情况下,可以检查 MoveNext() 是否实际成功(它返回一个布尔值),否则对 Current 的调用可能会抛出。

Please also note that the order of a dicitonary is not specified. This means that the 'first' item may in fact be any key, may be different on subsequent calls (though in practice will not) and will certainly change as items are added and removed from the dictionary.

另请注意,未指定字典的顺序。这意味着“第一个”项目实际上可能是任何键,在后续调用中可能会有所不同(尽管实际上不会)并且肯定会随着项目的添加和从字典中删除而改变。

回答by Think Big

we can using linqtechnology

我们可以使用linq技术

var key = dic.Take(1).Select(d => d.Key).First()

or we can use another search

或者我们可以使用其他搜索

var myList = dic.Take(1).Where(d => d.Key.Contains("Heaven")).ToList();