Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Thêm chuỗi vào cuối StringCollection trong C#

Ví dụ 1

Sử dụng phương thức Add của 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("Các phần tử StringCollection...");
        foreach(Object ob in strCol) {
            Console.WriteLine(ob);
        }
        
        Console.WriteLine("Số phần tử = " + strCol.Count);
    }
}

Kết quả

Các phần tử StringCollection...
John
Tim
Gary
Katie
Andy
Amy
Scarlett
Jacob
Số phần tử = 8

Ví dụ 2

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("Các phần tử StringCollection...");
        foreach(Object ob in strCol) {
            Console.WriteLine(ob);
        }
    }
}

Kết quả

Các phần tử StringCollection...
One
Two
Three
Four
Five