字典索引超出范围 [英] Dictionary Index out of range
本文介绍了字典索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我将TValue初始化为10时,出现索引超出范围错误.因此元素应该存在.
如果(!sportsDictionary.ContainsKey(sports.Team)) { sportsDictionary.Add(sports.Team,new List< SportsObject>(10)); } 别的 { sportsDictionary [sports.Team] [Team.sportsId] =体育; //错误:索引超出范围 } }
pianoboyCoder
解决方案
过去,缓冲区溢出一直是一个大问题.结果,所有.NET集合类都将检查您提供的索引是否有效.不是.
一个常见的问题是索引以0而不是1开头.但是,要给您任何真实的信息,我们需要知道在任何给定时间索引是什么.
>
I am getting an index out of range error, when I initialize the TValue to 10. So the elements should exist.
if (!sportsDictionary.ContainsKey(sports.Team)) { sportsDictionary.Add(sports.Team, new List<SportsObject>(10)); } else { sportsDictionary[sports.Team][Team.sportsId] = sports; //Error: index out of range } }
pianoboyCoder
解决方案
Buffer overflows have been a big issue in the past. As a result all .NET collection classes will check if the index you give it is valid. It is not.
A common problem is that indexes start with 0, not 1. However to give you any real information we need to know what the indexes are at any given time.
这篇关于字典索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文