コレクションのまとめ
.NET Framework 1.xではSystem.Collections名前空間のArrayList、Hashtableが使われていたみたいだが、2.0からはSystem.Collections.Generic名前空間のList
List
- Count
- Add()
- AddRange()
- Clear()
- Contains()
- IndexOf()
- Reverse()
- Sort()
- ToArray()
また、L[0]のようにインデクシング可能。スライシングは恐らく不可能。
Dictionary
- Count
- Keys
- Values
- Add()
- Clear()
- ContainsKey()
- ContainsValue()
- Remove()
// test.cs using System; using System.Collections.Generic; public class Test { static void Main() { Dictionary<string, string> dic = new Dictionary<string, string>(); dic["1"] = "a"; // もしくは、dic.Add("1", "a"); dic["2"] = "b"; Console.WriteLine(dic["1"]); //Console.WriteLine(dic["3"]); // System.Collections.Generic.KeyNotFoundException例外が発生 foreach (KeyValuePair<string, string> kv in dic) { Console.WriteLine("{0}:{1}", kv.Key, kv.Value); } } } // 実行結果 a 1:a 2:b
ビルドはコマンドラインで行う場合、csc test.csで行う。VisualStudioをインストールするとメニューの中に環境設定されたコマンドプロンプトのプログラムが追加されるのでそれを使用する。実行は、作成されたtest.exeを実行する。もちろん実行には、.NET環境のインストールが必要。