当前位置: 首页 > 工具软件 > TKey > 使用案例 >

按索引index取出指定位置泛型字典Dictionary的key和value的方法举例

欧旻
2023-12-01
        public static TKey DictionaryKeyByIndex<TKey, TValue>(Dictionary<TKey, TValue> dic, int index)
        {
            var res = default(TKey);
            try
            {
                res = dic.Keys.ToArray()[index];
            }
            catch
            {
                // ignored
            }
            return res;
        }
        public static TValue DictionaryValueByIndex<TKey, TValue>(Dictionary<TKey, TValue> dic, int index)
        {
            var res = default(TValue);
            try
            {
                res = dic.Values.ToArray()[index];
            }
            catch
            {
                // ignored
            }
            return res;
        }

 类似资料: