StringCollection 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
문자열 컬렉션을 나타냅니다.
public ref class StringCollection : System::Collections::IList
public class StringCollection : System.Collections.IList
[System.Serializable]
public class StringCollection : System.Collections.IList
type StringCollection = class
interface ICollection
interface IEnumerable
interface IList
[<System.Serializable>]
type StringCollection = class
interface IList
interface ICollection
interface IEnumerable
Public Class StringCollection
Implements IList
- 상속
-
StringCollection
- 파생
- 특성
- 구현
예제
다음 코드 예제에서는 몇 가지 속성 및 메서드를 StringCollection보여 줍니다.
using System;
using System.Collections;
using System.Collections.Specialized;
public class SamplesStringCollection {
public static void Main() {
// Create and initializes a new StringCollection.
StringCollection myCol = new StringCollection();
// Add a range of elements from an array to the end of the StringCollection.
String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" };
myCol.AddRange( myArr );
// Display the contents of the collection using foreach. This is the preferred method.
Console.WriteLine( "Displays the elements using foreach:" );
PrintValues1( myCol );
// Display the contents of the collection using the enumerator.
Console.WriteLine( "Displays the elements using the IEnumerator:" );
PrintValues2( myCol );
// Display the contents of the collection using the Count and Item properties.
Console.WriteLine( "Displays the elements using the Count and Item properties:" );
PrintValues3( myCol );
// Add one element to the end of the StringCollection and insert another at index 3.
myCol.Add( "* white" );
myCol.Insert( 3, "* gray" );
Console.WriteLine( "After adding \"* white\" to the end and inserting \"* gray\" at index 3:" );
PrintValues1( myCol );
// Remove one element from the StringCollection.
myCol.Remove( "yellow" );
Console.WriteLine( "After removing \"yellow\":" );
PrintValues1( myCol );
// Remove all occurrences of a value from the StringCollection.
int i = myCol.IndexOf( "RED" );
while ( i > -1 ) {
myCol.RemoveAt( i );
i = myCol.IndexOf( "RED" );
}
// Verify that all occurrences of "RED" are gone.
if ( myCol.Contains( "RED" ) )
Console.WriteLine( "*** The collection still contains \"RED\"." );
Console.WriteLine( "After removing all occurrences of \"RED\":" );
PrintValues1( myCol );
// Copy the collection to a new array starting at index 0.
String[] myArr2 = new String[myCol.Count];
myCol.CopyTo( myArr2, 0 );
Console.WriteLine( "The new array contains:" );
for ( i = 0; i < myArr2.Length; i++ ) {
Console.WriteLine( " [{0}] {1}", i, myArr2[i] );
}
Console.WriteLine();
// Clears the entire collection.
myCol.Clear();
Console.WriteLine( "After clearing the collection:" );
PrintValues1( myCol );
}
// Uses the foreach statement which hides the complexity of the enumerator.
// NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
public static void PrintValues1( StringCollection myCol ) {
foreach ( Object obj in myCol )
Console.WriteLine( " {0}", obj );
Console.WriteLine();
}
// Uses the enumerator.
// NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
public static void PrintValues2( StringCollection myCol ) {
StringEnumerator myEnumerator = myCol.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.WriteLine( " {0}", myEnumerator.Current );
Console.WriteLine();
}
// Uses the Count and Item properties.
public static void PrintValues3( StringCollection myCol ) {
for ( int i = 0; i < myCol.Count; i++ )
Console.WriteLine( " {0}", myCol[i] );
Console.WriteLine();
}
}
/*
This code produces the following output.
Displays the elements using foreach:
RED
orange
yellow
RED
green
blue
RED
indigo
violet
RED
Displays the elements using the IEnumerator:
RED
orange
yellow
RED
green
blue
RED
indigo
violet
RED
Displays the elements using the Count and Item properties:
RED
orange
yellow
RED
green
blue
RED
indigo
violet
RED
After adding "* white" to the end and inserting "* gray" at index 3:
RED
orange
yellow
* gray
RED
green
blue
RED
indigo
violet
RED
* white
After removing "yellow":
RED
orange
* gray
RED
green
blue
RED
indigo
violet
RED
* white
After removing all occurrences of "RED":
orange
* gray
green
blue
indigo
violet
* white
The new array contains:
[0] orange
[1] * gray
[2] green
[3] blue
[4] indigo
[5] violet
[6] * white
After clearing the collection:
*/
Imports System.Collections
Imports System.Collections.Specialized
Public Class SamplesStringCollection
Public Shared Sub Main()
' Create and initializes a new StringCollection.
Dim myCol As New StringCollection()
' Add a range of elements from an array to the end of the StringCollection.
Dim myArr() As String = {"RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED"}
myCol.AddRange(myArr)
' Display the contents of the collection using foreach. This is the preferred method.
Console.WriteLine("Displays the elements using foreach:")
PrintValues1(myCol)
' Display the contents of the collection using the enumerator.
Console.WriteLine("Displays the elements using the IEnumerator:")
PrintValues2(myCol)
' Display the contents of the collection using the Count and Item properties.
Console.WriteLine("Displays the elements using the Count and Item properties:")
PrintValues3(myCol)
' Add one element to the end of the StringCollection and insert another at index 3.
myCol.Add("* white")
myCol.Insert(3, "* gray")
Console.WriteLine("After adding ""* white"" to the end and inserting ""* gray"" at index 3:")
PrintValues1(myCol)
' Remove one element from the StringCollection.
myCol.Remove("yellow")
Console.WriteLine("After removing ""yellow"":")
PrintValues1(myCol)
' Remove all occurrences of a value from the StringCollection.
Dim i As Integer = myCol.IndexOf("RED")
While i > - 1
myCol.RemoveAt(i)
i = myCol.IndexOf("RED")
End While
' Verify that all occurrences of "RED" are gone.
If myCol.Contains("RED") Then
Console.WriteLine("*** The collection still contains ""RED"".")
End If
Console.WriteLine("After removing all occurrences of ""RED"":")
PrintValues1(myCol)
' Copy the collection to a new array starting at index 0.
Dim myArr2(myCol.Count) As String
myCol.CopyTo(myArr2, 0)
Console.WriteLine("The new array contains:")
For i = 0 To myArr2.Length - 1
Console.WriteLine(" [{0}] {1}", i, myArr2(i))
Next i
Console.WriteLine()
' Clears the entire collection.
myCol.Clear()
Console.WriteLine("After clearing the collection:")
PrintValues1(myCol)
End Sub
' Uses the foreach statement which hides the complexity of the enumerator.
' NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
Public Shared Sub PrintValues1(myCol As StringCollection)
Dim obj As [Object]
For Each obj In myCol
Console.WriteLine(" {0}", obj)
Next obj
Console.WriteLine()
End Sub
' Uses the enumerator.
' NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
Public Shared Sub PrintValues2(myCol As StringCollection)
Dim myEnumerator As StringEnumerator = myCol.GetEnumerator()
While myEnumerator.MoveNext()
Console.WriteLine(" {0}", myEnumerator.Current)
End While
Console.WriteLine()
End Sub
' Uses the Count and Item properties.
Public Shared Sub PrintValues3(myCol As StringCollection)
Dim i As Integer
For i = 0 To myCol.Count - 1
Console.WriteLine(" {0}", myCol(i))
Next i
Console.WriteLine()
End Sub
End Class
'This code produces the following output.
'
'Displays the elements using foreach:
' RED
' orange
' yellow
' RED
' green
' blue
' RED
' indigo
' violet
' RED
'
'Displays the elements using the IEnumerator:
' RED
' orange
' yellow
' RED
' green
' blue
' RED
' indigo
' violet
' RED
'
'Displays the elements using the Count and Item properties:
' RED
' orange
' yellow
' RED
' green
' blue
' RED
' indigo
' violet
' RED
'
'After adding "* white" to the end and inserting "* gray" at index 3:
' RED
' orange
' yellow
' * gray
' RED
' green
' blue
' RED
' indigo
' violet
' RED
' * white
'
'After removing "yellow":
' RED
' orange
' * gray
' RED
' green
' blue
' RED
' indigo
' violet
' RED
' * white
'
'After removing all occurrences of "RED":
' orange
' * gray
' green
' blue
' indigo
' violet
' * white
'
'The new array contains:
' [0] orange
' [1] * gray
' [2] green
' [3] blue
' [4] indigo
' [5] violet
' [6] * white
'
'After clearing the collection:
'
설명
StringCollection 는 null 유효한 값으로 허용되고 중복 요소를 허용합니다.
문자열 비교는 대/소문자를 구분합니다.
이 컬렉션의 요소는 정수 인덱스로 액세스할 수 있습니다. 이 컬렉션의 인덱스는 0부터 시작합니다.
생성자
| Name | Description |
|---|---|
| StringCollection() |
StringCollection 클래스의 새 인스턴스를 초기화합니다. |
속성
| Name | Description |
|---|---|
| Count |
에 포함된 StringCollection문자열 수를 가져옵니다. |
| IsReadOnly |
StringCollection 읽기 전용인지 여부를 나타내는 값을 가져옵니다. |
| IsSynchronized |
StringCollection 대한 액세스가 동기화되는지 여부를 나타내는 값을 가져옵니다(스레드로부터 안전). |
| Item[Int32] |
지정된 인덱스에서 요소를 가져오거나 설정합니다. |
| SyncRoot |
StringCollection대한 액세스를 동기화하는 데 사용할 수 있는 개체를 가져옵니다. |
메서드
| Name | Description |
|---|---|
| Add(String) |
의 끝에 문자열을 추가합니다 StringCollection. |
| AddRange(String[]) |
문자열 배열의 요소를 .의 끝에 복사합니다 StringCollection. |
| Clear() |
에서 모든 문자열을 제거합니다 StringCollection. |
| Contains(String) |
지정된 문자열이 .에 StringCollection있는지 여부를 확인합니다. |
| CopyTo(String[], Int32) |
대상 배열의 지정된 인덱스에서 시작하여 전체 StringCollection 값을 문자열의 1차원 배열에 복사합니다. |
| Equals(Object) |
지정한 개체와 현재 개체가 같은지 여부를 확인합니다. (다음에서 상속됨 Object) |
| GetEnumerator() |
를 StringEnumerator 반복하는 값을 반환합니다 StringCollection. |
| GetHashCode() |
기본 해시 함수로 작동합니다. (다음에서 상속됨 Object) |
| GetType() |
현재 인스턴스의 Type 가져옵니다. (다음에서 상속됨 Object) |
| IndexOf(String) |
지정된 문자열을 검색하고 0부터 시작하는 인덱스( StringCollection0부터 시작)를 반환합니다. |
| Insert(Int32, String) |
지정된 인덱스에 문자열을 StringCollection 삽입합니다. |
| MemberwiseClone() |
현재 Object단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
| Remove(String) |
에서 특정 문자열의 첫 번째 항목을 제거합니다 StringCollection. |
| RemoveAt(Int32) |
지정된 인덱 StringCollection스의 문자열을 제거합니다. |
| ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |
명시적 인터페이스 구현
| Name | Description |
|---|---|
| ICollection.CopyTo(Array, Int32) |
대상 배열의 지정된 인덱스에서 시작하여 호환되는 1차원StringCollection으로 전체를 Array 복사합니다. |
| IEnumerable.GetEnumerator() |
를 IEnumerator 반복하는 값을 반환합니다 StringCollection. |
| IList.Add(Object) |
의 끝에 개체를 추가합니다 StringCollection. |
| IList.Contains(Object) |
요소가 .에 StringCollection있는지 여부를 확인합니다. |
| IList.IndexOf(Object) |
지정한 Object 인덱스(0부터 시작)를 검색하여 전체에서 첫 번째 항목의 인덱스(0부터 시작)를 반환합니다 StringCollection. |
| IList.Insert(Int32, Object) |
지정된 인덱스에 요소를 StringCollection 삽입합니다. |
| IList.IsFixedSize |
개체의 크기가 고정되어 있는지 여부를 StringCollection 나타내는 값을 가져옵니다. |
| IList.IsReadOnly |
개체가 읽기 전용인지 여부를 StringCollection 나타내는 값을 가져옵니다. |
| IList.Item[Int32] |
지정된 인덱스에서 요소를 가져오거나 설정합니다. |
| IList.Remove(Object) |
에서 특정 개체의 첫 번째 항목을 제거합니다 StringCollection. |
확장명 메서드
| Name | Description |
|---|---|
| AsParallel(IEnumerable) |
쿼리의 병렬 처리를 사용하도록 설정합니다. |
| AsQueryable(IEnumerable) |
IEnumerable IQueryable변환합니다. |
| Cast<TResult>(IEnumerable) |
IEnumerable 요소를 지정된 형식으로 캐스팅합니다. |
| OfType<TResult>(IEnumerable) |
지정된 형식에 따라 IEnumerable 요소를 필터링합니다. |
적용 대상
스레드 보안
이 형식의 공용 정적(Shared Visual Basic) 멤버는 스레드로부터 안전합니다. 모든 인스턴스 멤버는 스레드로부터 안전하게 보호되지 않습니다.
이 구현은 동기화된(스레드로부터 안전한) 래퍼를 StringCollection제공하지 않지만 파생 클래스는 속성을 사용하여 StringCollection 자체 동기화된 버전을 SyncRoot 만들 수 있습니다.
컬렉션을 열거하는 것은 본질적으로 스레드로부터 안전한 프로시저가 아닙니다. 컬렉션이 동기화된 경우에도 다른 스레드는 컬렉션을 수정할 수 있으므로 열거자가 예외를 throw합니다. 열거 중 스레드 안전을 보장하기 위해 전체 열거 중에 컬렉션을 잠그거나 다른 스레드의 변경으로 인한 예외를 catch할 수 있습니다.