cipher = new Dictionary<char,int>;
cipher.Add( 'a', 324 );
cipher.Add( 'b', 553 );
cipher.Add( 'c', 915 );
如何获取第二个元素?例如,我想要像这样的内容:KeyValuePair pair = cipher[1]
根据 coop 的建议,使用 List,事情正在运作中:
其中 pair 包含 ( 'b', 553 )
List<KeyValuePair<char, int>> cipher = new List<KeyValuePair<char, int>>();
cipher.Add( new KeyValuePair<char, int>( 'a', 324 ) );
cipher.Add( new KeyValuePair<char, int>( 'b', 553 ) );
cipher.Add( new KeyValuePair<char, int>( 'c', 915 ) );
KeyValuePair<char, int> pair = cipher[ 1 ];
假设我的正确理解是项目会按照它们添加的顺序保留在列表中,我认为我可以只使用列表List
而不是建议使用的SortedList
。