ListはAdd順らしい
前の続き。
DictionaryはAdd順で列挙してくれるとは限らないらしい(なんせググって見つかったことである)。そしてさらにもうちょいググり続けるとListはAdd順で列挙してくれるらしい???(だんだんあやふやになってきたorz)
じゃ、Dictionaryの代わりにListにするかってことで
var dic = new List<KeyValuePair<int, string>>();
dic.Add(new KeyValuePair<int, string>(8, "伊丹"));
dic.Add(new KeyValuePair<int, string>(4, "新伊丹"));
dic.Add(new KeyValuePair<int, string>(6, "稲野"));
dic.Add(new KeyValuePair<int, string>(3, "塚口"));
・・・・
new KeyValuePair<int, string>がいっぱいあってうざったい!!!!!!
Dictionaryならすっきりだったのに!!!!
てなわけでKeyValuePairのListに特化したClassを作ってみた。
class ListKeyValuePair<T1,T2> : List<KeyValuePair<T1, T2>> {
public void Add(T1 key,T2 value) {
base.Add(new KeyValuePair<T1, T2>(key, value));
}
}
うん、やってることは単純だなぁ。これつかうと
var dic = new ListKeyValuePair<int, string>();
dic.Add(8, "伊丹");
dic.Add(4, "新伊丹");
dic.Add(6, "稲野");
dic.Add(3, "塚口");
よし、これですっきし。
これ実戦投入する気はない(お堅い現場でつので)が、やるだけやったのですっきし。