ในการเพิ่มสตริงที่ส่วนท้ายของ StringCollection รหัสจะเป็นดังนี้ -
ตัวอย่าง
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol = new StringCollection();
strCol.Add("John");
strCol.Add("Tim");
strCol.Add("Gary");
strCol.Add("Katie");
strCol.Add("Andy");
strCol.Add("Amy");
strCol.Add("Scarlett");
strCol.Add("Jacob");
Console.WriteLine("Elements in StringCollection...");
foreach(Object ob in strCol) {
Console.WriteLine(ob);
}
Console.WriteLine("Count of elements = "+strCol.Count);
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Elements in StringCollection... John Tim Gary Katie Andy Amy Scarlett Jacob Count of elements = 8
ตัวอย่าง
เรามาดูตัวอย่างกัน −
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol = new StringCollection();
strCol.Add("One");
strCol.Add("Two");
strCol.Add("Three");
strCol.Add("Four");
strCol.Add("Five");
Console.WriteLine("Elements in StringCollection...");
foreach(Object ob in strCol) {
Console.WriteLine(ob);
}
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Elements in StringCollection... One Two Three Four Five