必要なのは、リストのような動的データ構造です。
ジェネリック (つまりリスト) または非ジェネリック (つまりリスト) バージョンのいずれかを使用できます。リストを使用すると、アイテムを動的に追加または挿入したり、インデックスを決定したり、必要に応じてアイテムを削除したりできます。
リスト操作を使用していると、リストのサイズは動的に拡大/縮小します。
画像が Image 型のオブジェクトとして表されていると仮定すると、次のような List を使用できます。
// instantiation of an empty list
List<Image> list = new List<Image>();
// create ten images and add them to the list (append at the end of the list at each iteration)
for (int i = 0; i <= 9; i++) {
Image img = new Image();
list.Add(img);
}
// remove every second image from the list starting at the beginning
for (int i = 0; i <= 9; i += 2) {
list.RemoveAt(i);
}
// insert a new image at the first position in the list
Image img1 = new Image();
list.Insert(0, img1);
// insert a new image at the first position in the list
IMage img2 = new Image();
list.Insert(0, img2);
辞書を使用した代替アプローチ:
Dictionary<string, Image> dict = new Dictionary<string, Image>();
for (int i = 0; i <= 9; i++) {
Image img = new Image();
// suppose img.Name is an unique identifier then it is used as the images keys
// in this dictionary. You create a direct unique mapping between the images name
// and the image itself.
dict.Add(img.Name, img);
}
// that's how you would use the unique image identifier to refer to an image
Image img1 = dict["Image1"];
Image img2 = dict["Image2"];
Image img3 = dict["Image3"];