怎么随便搞个网站,建企业网站 硬件,外贸模板网站深圳,绵阳网站建设优化一#xff1a;背景 1. 讲故事昨天在 StackOverflow 上看到一个很有趣的问题#xff0c;说: 你会几种遍历字典的方式#xff0c;然后跟帖就是各种奇葩的回答#xff0c;挺有意思#xff0c;马上就要国庆了#xff0c;娱乐娱乐吧#xff0c;说说这种挺无聊的问题?????… 一背景 1. 讲故事昨天在 StackOverflow 上看到一个很有趣的问题说: 你会几种遍历字典的方式然后跟帖就是各种奇葩的回答挺有意思马上就要国庆了娱乐娱乐吧说说这种挺无聊的问题????????????。二使用 foreach 遍历 为了方便演示先上一段测试代码var dict new Dictionaryint, string(){[10] A10,[20] A20,[30] A30,[40] A40,[50] A50};1. 直接 foreach dict如果要拿百分比说话估计有 50% 的小伙伴用这种方式为啥简单粗暴呗其他没什么好说的直接上代码foreach (var item in dict){Console.WriteLine($key{item.Key},value{item.Value});}这里的 item 是底层在 MoveNext 的过程中用 KeyValuePair 包装出来的如果你不信的话看下源码呗public bool MoveNext(){while ((uint)_index (uint)_dictionary._count){ref Entry reference ref _dictionary._entries[_index];if (reference.next -1){_current new KeyValuePairTKey, TValue(reference.key, reference.value);return true;}}}2. foreach 中 使用 KeyPairValue 解构刚才你也看到了 item 是 KeyValuePair 类型不过????????的是 netcore 对 KeyValuePair 进行了增强增加了 Deconstruct 函数用来解构 KeyValuePair代码如下public readonly struct KeyValuePairTKey, TValue{private readonly TKey key;private readonly TValue value;public TKey Key key;public TValue Value value;public KeyValuePair(TKey key, TValue value){this.key key;this.value value;}public void Deconstruct(out TKey key, out TValue value){key Key;value Value;}}有了这个解构函数你就可以在遍历的过程中直接拿到 keyvalue而不是包装的 KeyValuePair这在 netframework 中可是不行的哈实现代码如下foreach ((int key, string value) in dict){Console.WriteLine($key{key},value{value});}3. foreach keys前面的例子都是直接对 dict 进行 foreach其实你还可以对 dict.keys 进行 foreach 遍历然后通过遍历出的 key 对 dict 进行类索引器读取代码如下foreach (var key in dict.Keys){Console.WriteLine($key{key},value{dict[key]});}说到这里不知道你是否有一个潜意识那就是 dict 只能通过 foreach 进行遍历真相是不是这样的呢要寻找答案还是回头看一下 foreach 是如何进行遍历的。
public struct Enumerator : IEnumeratorKeyValuePairTKey, TValue, IDisposable, IEnumerator, IDictionaryEnumerator
{public bool MoveNext(){while ((uint)_index (uint)_dictionary._count){ref Entry reference ref _dictionary._entries[_index];if (reference.next -1){_current new KeyValuePairTKey, TValue(reference.key, reference.value);return true;}}_index _dictionary._count 1;_current default(KeyValuePairTKey, TValue);return false;}
}仔细看这个 while 循环你就应该明白本质上它也是对 entries 数组进行遍历那底层都用了 while我是不是可以用 for 来替换然后循环 dict 呢哈哈反正就是模仿呗。三使用 for 遍历 为了把 MoveNext 中的代码模拟出来重点在于这条语句ref Entry reference ref _dictionary._entries[_index];, 其实很简单_entries 数组内容的提取可以用 Linq 的 ElementAt 方法是不是~~~ 改造后的代码如下for (int i 0; i dict.Count; i){(int key, string value) dict.ElementAt(i);Console.WriteLine($key{key},value{dict[key]});}接下来是不是很好奇这个 ElementAt 扩展方法是如何实现的一起看看源码吧。public static TSource ElementAtTSource(this IEnumerableTSource source, int index){IListTSource list source as IListTSource;if (list ! null){return list[index];}if (index 0){using (IEnumeratorTSource enumerator source.GetEnumerator()){while (enumerator.MoveNext()){if (index 0){return enumerator.Current;}index--;}}}}从上面代码可以看到如果当前的 source 没有实现 IList 接口的话那就是一个巨大的坑每一次执行 ElementAt 方法最坏时间复杂度都是 O(N)就拿刚才的 for循环来说它的最坏时间复杂度就是 O(n!) 是不是比你想象的要恐怖的多教训就是多实践多看看源码~四总结 这篇列举了 4 种遍历 dict 的方式不知你会用到哪几种要注意的是最后 ElementAt 对 Source 判别上的大坑一定要明白不要想当然的以为就是 O(N) 好了更多的 遍历方式 欢迎补充