Collection의 지정된 인덱스에 있는 요소를 가져오거나 설정하려면 코드는 다음과 같습니다. -
예
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main() {
Collection<string> col = new Collection<string>();
col.Add("Laptop");
col.Add("Desktop");
col.Add("Notebook");
col.Add("Ultrabook");
col.Add("Tablet");
col.Add("Headphone");
col.Add("Speaker");
Console.WriteLine("Elements in Collection...");
foreach(string str in col) {
Console.WriteLine(str);
}
Console.WriteLine("Element at index 3 = " + col[3]);
Console.WriteLine("Element at index 4 = " + col[4]);
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
Elements in Collection... Laptop Desktop Notebook Ultrabook Tablet Headphone Speaker Element at index 3 = Ultrabook Element at index 4 = Tablet
예
다른 예를 살펴보겠습니다 -
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main() {
Collection<string> col = new Collection<string>();
col.Add("Laptop");
col.Add("Desktop");
col.Add("Notebook");
col.Add("Ultrabook");
col.Add("Tablet");
col.Add("Headphone");
col.Add("Speaker");
Console.WriteLine("Elements in Collection...");
foreach(string str in col) {
Console.WriteLine(str);
}
Console.WriteLine("Element at index 3 = " + col[3]);
col[3] = "SSD";
Console.WriteLine("Element at index 3 (Updated) = " + col[3]);
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
Elements in Collection... Laptop Desktop Notebook Ultrabook Tablet Headphone Speaker Element at index 3 = Ultrabook Element at index 3 (Updated) = SSD