StringCollection 클래스는 문자열 컬렉션을 나타냅니다. 다음은 StringCollection 클래스의 속성입니다 -
| Sr.no | 속성 및 설명 |
|---|---|
| 1 | 카운트 OrderedDictionary 컬렉션에 포함된 키/값 쌍의 수를 가져옵니다. |
| 2 | 읽기 전용 StringCollection이 읽기 전용인지 여부를 나타내는 값을 가져옵니다. |
| 3 | 동기화됨 StringCollection에 대한 액세스가 동기화되었는지(스레드 안전) 여부를 나타내는 값을 가져옵니다. |
| 4 | 항목[Int32] 지정된 인덱스에 있는 요소를 가져오거나 설정합니다. |
| 5 | SyncRoot StringCollection에 대한 액세스를 동기화하는 데 사용할 수 있는 개체를 가져옵니다. |
다음은 StringCollection 클래스의 메소드입니다 -
| Sr.no | 방법 및 설명 |
|---|---|
| 1 | 추가(문자열) StringCollection 끝에 문자열을 추가합니다. |
| 2 | AddRange(문자열[]) 문자열 배열의 요소를 StringCollection의 끝에 복사합니다. |
| 3 | 지우기() StringCollection에서 모든 문자열을 제거합니다. |
| 4 | 포함(문자열) 지정된 문자열이 StringCollection에 있는지 여부를 확인합니다. |
| 5 | CopyTo(String[],Int32) 대상 배열의 지정된 인덱스에서 시작하여 전체 StringCollection 값을 1차원 문자열 배열에 복사합니다. |
| 6 | 같음(객체) 지정된 개체가 현재 개체와 같은지 여부를 확인합니다. (Object에서 상속됨) |
| 7 | GetEnumerator() StringCollection을 반복하는 StringEnumerator를 반환합니다. |
이제 몇 가지 예를 살펴보겠습니다.
두 StringCollection 객체가 같은지 확인하려면 코드는 다음과 같습니다. -
예
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) {
Console.WriteLine(res);
}
Console.WriteLine("Both the String Collections are equal? = "+strCol1.Equals(strCol2));
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
StringCollection1 elements... Accessories Books Electronics StringCollection2 elements... Accessories Books Electronics Both the String Collections are equal? = False
지정된 문자열이 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("Does the specified string is in the StringCollection? = "+stringCol.Contains("800"));
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
Array elements... 100 200 300 400 500 Does the specified string is in the StringCollection? = False