gpt4 book ai didi

c# - C# SortedList 如何按值获取键?

转载 作者:太空狗 更新时间:2023-10-30 00:11:19 27 4
gpt4 key购买 nike

有一个SortedList

slLanguage = new SortedList();
slLanguage.Add("Bahasa","id-ID");
slLanguage.Add("Chinese Simplified(中文简体)","zh-CN");
slLanguage.Add("Chinese Traditional(中文繁體)","zh-TW");
slLanguage.Add("Kazakh","kk-KZ");
slLanguage.Add("Russian(русский)","ru-RU");
slLanguage.Add("Vietnamese(Việt)","vi-VN");
slLanguage.Add("English", "en-US");

如何按值获取 key ?

例如:获取item key“zh-CN”

最佳答案

如果您想从一个值中获取键,您可以使用SortedList.IndexOfValue(object value) 来获取您指定的值的索引。然后,使用 SortedList.GetKey(int index) 从我们刚刚收集的值的索引中返回一个键作为 object

示例

SortedList slLanguage = new SortedList(); //Initializes a new SortedList of name slLanguage
//Add the keys and their values to the list
slLanguage.Add("Bahasa", "id-ID");
slLanguage.Add("Chinese Simplified(中文简体)", "zh-CN");
slLanguage.Add("Chinese Traditional(中文繁體)", "zh-TW");
slLanguage.Add("Kazakh", "kk-KZ");
slLanguage.Add("Russian(русский)", "ru-RU");
slLanguage.Add("Vietnamese(Việt)", "vi-VN");
slLanguage.Add("English", "en-US");
//
object returnedKey = slLanguage.GetKey(slLanguage.IndexOfValue("zh-CN")); //Gets the key from zh-CN as returnedKey of type object

谢谢,
希望这对您有所帮助:)

关于c# - C# SortedList 如何按值获取键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13795130/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com