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

Lớp StringCollection trong C#

Lớp StringCollection đại diện cho một tập hợp các chuỗi. Dưới đây là các thuộc tính và phương thức chính.

Các thuộc tính của StringCollection

STTThuộc tính & Mô tả
1Count
Lấy số cặp key/value chứa trong OrderedDictionary collection. (Lưu ý: tài liệu gốc có vẻ nhầm lẫn, StringCollection không có key/value, Count trả về số chuỗi).
2IsReadOnly
Cho biết StringCollection có chỉ đọc hay không.
3IsSynchronized
Cho biết truy cập StringCollection có đồng bộ (thread-safe) hay không.
4Item[Int32]
Lấy hoặc đặt phần tử tại index chỉ định.
5SyncRoot
Lấy object dùng để đồng bộ truy cập StringCollection.

Các phương thức của StringCollection

STTPhương thức & Mô tả
1Add(String)
Thêm chuỗi vào cuối StringCollection.
2AddRange(String[])
Sao chép mảng chuỗi vào cuối StringCollection.
3Clear()
Xóa tất cả chuỗi khỏi StringCollection.
4Contains(String)
Xác định chuỗi chỉ định có trong StringCollection hay không.
5CopyTo(String[], Int32)
Sao chép toàn bộ StringCollection sang mảng 1 chiều từ index chỉ định.
6Equals(Object)
Xác định object chỉ định có bằng object hiện tại. (Kế thừa từ Object)
7GetEnumerator()
Trả về StringEnumerator để duyệt StringCollection.

Ví dụ 1: Kiểm tra hai StringCollection bằng nhau

using System;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        StringCollection strCol1 = new StringCollection();
        strCol1.Add("Accessories");
        strCol1.Add("Books");
        strCol1.Add("Electronics");
        
        Console.WriteLine("StringCollection1 elements...");
        foreach (string res in strCol1) {
            Console.WriteLine(res);
        }
        
        StringCollection strCol2 = new StringCollection();
        strCol2.Add("Accessories");
        strCol2.Add("Books");
        strCol2.Add("Electronics");
        
        Console.WriteLine("StringCollection2 elements...");
        foreach (string res in strCol1) { // lưu ý: code gốc duyệt strCol1
            Console.WriteLine(res);
        }
        
        Console.WriteLine("Hai String Collection bằng nhau? = " + strCol1.Equals(strCol2));
    }
}

Kết quả

StringCollection1 elements...
Accessories
Books
Electronics
StringCollection2 elements...
Accessories
Books
Electronics
Hai String Collection bằng nhau? = False

Ví dụ 2: Kiểm tra chuỗi có trong StringCollection

using System;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        StringCollection stringCol = new StringCollection();
        String[] arr = new String[] { "100", "200", "300", "400", "500" };
        
        Console.WriteLine("Array elements...");
        foreach (string res in arr) {
            Console.WriteLine(res);
        }
        
        stringCol.AddRange(arr);
        Console.WriteLine("Chuỗi '800' có trong StringCollection? = " + stringCol.Contains("800"));
    }
}

Kết quả

Array elements...
100
200
300
400
500
Chuỗi '800' có trong StringCollection? = False