StringCollection 클래스

정의

문자열 컬렉션을 나타냅니다.

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.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;

void PrintValues1( StringCollection^ myCol );
void PrintValues2( StringCollection^ myCol );
void PrintValues3( StringCollection^ myCol );

int main()
{
   
   // Create and initializes a new StringCollection.
   StringCollection^ myCol = gcnew StringCollection;
   
   // Add a range of elements from an array to the end of the StringCollection.
   array<String^>^myArr = {"RED","orange","yellow","RED","green","blue","RED","indigo","violet","RED"};
   myCol->AddRange( myArr );
   
   // Display the contents of the collection using for each. This is the preferred method.
   Console::WriteLine( "Displays the elements using for each:" );
   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.
   array<String^>^myArr2 = gcnew array<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 for each statement which hides the complexity of the enumerator.
// NOTE: The for each statement is the preferred way of enumerating the contents of a collection.
void PrintValues1( StringCollection^ myCol )  {
   for each ( Object^ obj in myCol )
      Console::WriteLine( "   {0}", obj );
   Console::WriteLine();
}

// Uses the enumerator. 
void PrintValues2( StringCollection^ myCol )
{
   StringEnumerator^ myEnumerator = myCol->GetEnumerator();
   while ( myEnumerator->MoveNext() )
      Console::WriteLine( "   {0}", myEnumerator->Current );

   Console::WriteLine();
}


// Uses the Count and Item properties.
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 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:

*/
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:
'

설명

StringCollectionnull 유효한 값으로 허용되고 중복 요소를 허용합니다.

문자열 비교는 대/소문자를 구분합니다.

이 컬렉션의 요소는 정수 인덱스로 액세스할 수 있습니다. 이 컬렉션의 인덱스는 0부터 시작 합니다.

생성자

StringCollection()

StringCollection 클래스의 새 인스턴스를 초기화합니다.

속성

Count

StringCollection에 포함된 문자열 수를 가져옵니다.

IsReadOnly

StringCollection가 읽기 전용인지 여부를 나타내는 값을 가져옵니다.

IsSynchronized

StringCollection에 대한 액세스가 동기화되어 스레드로부터 안전하게 보호되는지를 나타내는 값을 가져옵니다.

Item[Int32]

지정한 인덱스에 있는 요소를 가져오거나 설정합니다.

SyncRoot

StringCollection에 대한 액세스를 동기화하는 데 사용할 수 있는 개체를 가져옵니다.

메서드

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)

지정한 문자열을 검색하고 StringCollection 내에서 처음 나오는 0부터 시작하는 인덱스를 반환합니다.

Insert(Int32, String)

지정한 인덱스에 있는 StringCollection에 문자열을 삽입합니다.

MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
Remove(String)

StringCollection에서 맨 처음 발견되는 특정 문자열을 제거합니다.

RemoveAt(Int32)

StringCollection의 지정한 인덱스에 있는 문자열을 제거합니다.

ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

명시적 인터페이스 구현

ICollection.CopyTo(Array, Int32)

대상 배열의 지정된 인덱스에서 시작하여 전체 StringCollection을 호환되는 1차원 Array에 복사합니다.

IEnumerable.GetEnumerator()

IEnumerator을 반복하는 StringCollection를 반환합니다.

IList.Add(Object)

개체를 StringCollection의 끝 부분에 추가합니다.

IList.Contains(Object)

StringCollection에 요소가 있는지 여부를 확인합니다.

IList.IndexOf(Object)

지정한 Object를 검색하고, 전체 StringCollection 내에서 처음 나오는 0부터 시작하는 인덱스를 반환합니다.

IList.Insert(Int32, Object)

StringCollection의 지정된 인덱스에 요소를 삽입합니다.

IList.IsFixedSize

StringCollection 개체의 크기가 고정되어 있는지 여부를 나타내는 값을 가져옵니다.

IList.IsReadOnly

StringCollection 개체가 읽기 전용인지 여부를 나타내는 값을 가져옵니다.

IList.Item[Int32]

지정한 인덱스에 있는 요소를 가져오거나 설정합니다.

IList.Remove(Object)

StringCollection에서 맨 처음 발견되는 특정 개체를 제거합니다.

확장 메서드

Cast<TResult>(IEnumerable)

IEnumerable의 요소를 지정된 형식으로 캐스팅합니다.

OfType<TResult>(IEnumerable)

지정된 형식에 따라 IEnumerable의 요소를 필터링합니다.

AsParallel(IEnumerable)

쿼리를 병렬화할 수 있도록 합니다.

AsQueryable(IEnumerable)

IEnumerableIQueryable로 변환합니다.

적용 대상

스레드 보안

공용 정적 (Shared Visual Basic의)이 형식의 멤버는 스레드로부터 안전 합니다. 인스턴스 구성원은 스레드로부터의 안전성이 보장되지 않습니다.

이 구현은 동기화된(스레드로부터 안전한) 래퍼를 StringCollection제공하지 않지만 파생 클래스는 속성을 사용하여 SyncRoot 자체 동기화된 버전을 StringCollection 만들 수 있습니다.

컬렉션을 열거 되지 본질적으로 스레드로부터 안전한 프로시저가 있습니다. 컬렉션이 동기화되어 있을 때 다른 스레드에서 해당 컬렉션을 수정할 수 있으므로 이렇게 되면 열거자에서 예외가 throw됩니다. 열거하는 동안 스레드로부터 안전을 보장하려면 전체 열거를 수행하는 동안 컬렉션을 잠그거나 다른 스레드에서 변경된 내용으로 인해 발생한 예외를 catch하면 됩니다.

추가 정보