Collection 클래스

정의

Visual Basic Collection은 하나의 단위로 참조할 수 있는 정렬된 항목 집합입니다.

public ref class Collection sealed : System::Collections::IList
public ref class Collection sealed : System::Collections::IList, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
public sealed class Collection : System.Collections.IList
[System.Serializable]
public sealed class Collection : System.Collections.IList, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
type Collection = class
    interface ICollection
    interface IEnumerable
    interface IList
type Collection = class
    interface ICollection
    interface IList
[<System.Serializable>]
type Collection = class
    interface ICollection
    interface IList
    interface ISerializable
    interface IDeserializationCallback
Public NotInheritable Class Collection
Implements IList
Public NotInheritable Class Collection
Implements IDeserializationCallback, IList, ISerializable
상속
Collection
특성
구현

예제

다음 예제에서는 개체 names 와 사용자가 컬렉션에 개체(이름)를 추가할 수 있는 대화 상자를 만듭니다Collection. 그런 다음 컬렉션에 이름을 표시하고 개체 자체를 삭제 Collection 하지 않고 컬렉션을 비웁니다.

작동 방식을 확인하려면 프로젝트 메뉴에서 클래스 추가 명령을 선택하고 모듈 수준 nameClass (형식Public instanceName)에서 라는 instanceName 공용 변수를 선언하여 각 instance 이름을 저장합니다. 기본 이름을 로 둡 nameClass니다. 다음 코드를 복사하여 다른 모듈의 일반 섹션에 붙여넣은 다음 다른 절차의 문 classNamer 으로 시작합니다. (이 예제에서는 클래스를 지 원하는 호스트 애플리케이션 에서만 작동 합니다.)

Public Class nameClass
    Public instanceName As String
End Class
Sub classNamer()
    ' Create a Visual Basic Collection object.
    Dim names As New Microsoft.VisualBasic.Collection()
    Dim key As Integer
    Dim msg As String
    Dim name As String
    Dim nameList As String = ""
    ' 1. Get names from the user to add to the collection.
    Do
        Dim inst As New nameClass()
        key += 1
        msg = "Please enter a name for this object." & vbCrLf &
              "Press Cancel to see names in collection."
        name = InputBox(msg, "Name the Collection items")
        inst.instanceName = name
        ' If user entered a name, add it to the collection.
        If inst.instanceName <> "" Then
            names.Add(inst, CStr(key))
        End If
    Loop Until name = ""
    ' 2. Create and display a list of names from the collection.
    For Each oneInst As nameClass In names
        nameList &= oneInst.instanceName & vbCrLf
    Next oneInst
    MsgBox(nameList, , "Instance Names in names Collection")
    ' 3. Remove elements from the collection without disposing of the collection.
    For count As Integer = 1 To names.Count
        names.Remove(1)
        ' Since Visual Basic collections are reindexed automatically, 
        ' remove the first member on each iteration.
    Next count
End Sub

설명

참고

가능하면 Visual Basic Collection대신 네임스페이스 또는 네임스페이 System.Collections.Concurrent 스의 제네릭 컬렉션을 System.Collections.Generic 사용해야 합니다. 자세한 내용은 컬렉션 - C# 또는 컬렉션 - Visual Basic을 참조하세요.

Visual Basic Collection 개체는 관련 항목 그룹을 단일 개체로 참조하는 편리한 방법을 제공합니다. 컬렉션의 항목 또는 요소는 컬렉션에 있다는 사실만 관련되어야 합니다. 컬렉션의 요소는 동일한 데이터 형식을 공유할 필요가 없습니다.

다음 예제와 같이 다른 개체를 만드는 것과 동일한 방식으로 컬렉션을 만들 수 있습니다.

Dim coll As New Microsoft.VisualBasic.Collection()  

컬렉션을 만든 후에는 다음 중 원하는 작업을 수행할 수 있습니다.

  • 메서드를 사용하여 요소를 추가합니다 Add .

  • 메서드를 사용하여 요소를 제거합니다 Remove .

  • 메서드를 사용하여 모든 요소를 제거합니다 Clear .

  • 속성과 함께 Count 컬렉션에 포함된 요소 수를 확인합니다.

  • 특정 요소가 메서드와 함께 Contains 있는지 확인합니다.

  • 속성을 사용하여 컬렉션에서 특정 요소를 반환합니다 Item[] .

  • For Each...를 사용하여 전체 컬렉션을 반복합니다 . 다음 문입니다.

    참고

    Visual Basic Collection 개체의 기능은 Visual Basic 6.0의 Collection 개체와 동일하지만 두 개체는 COM 환경에서 상호 운용할 수 없습니다.

    주의

    Visual Basic Collection 을 반복하는 것은 스레드로부터 안전한 프로시저가 아닙니다. 컬렉션이 동기화되더라도 다른 스레드는 여전히 컬렉션을 수정할 수 있으므로 열거자가 예외를 throw합니다. 열거 중 스레드 안전을 보장하려면 컬렉션을 잠그거나 다른 스레드의 변경으로 인한 예외를 catch합니다. 프로그래밍 요소 잠금에 대한 자세한 내용은 SyncLock 문을 참조하세요.

생성자

Collection()

새 Visual Basic Collection 개체를 만들어 반환합니다.

속성

Count

컬렉션에 있는 요소 수를 가져옵니다.

Item[Int32]

위치나 키를 기준으로 Collection 개체의 특정 요소를 가져옵니다.

Item[Object]

위치나 키를 기준으로 Collection 개체의 특정 요소를 가져옵니다.

Item[String]

위치나 키를 기준으로 Collection 개체의 특정 요소를 가져옵니다.

메서드

Add(Object, String, Object, Object)

Collection 개체에 요소를 추가합니다.

Clear()

Visual Basic Collection 개체의 모든 요소를 삭제합니다.

Contains(String)

Visual Basic Collection 개체에 특정 키를 가진 요소가 포함되어 있는지 여부를 나타내는 값을 반환합니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetEnumerator()

컬렉션을 반복하는 열거자를 반환합니다.

GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

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

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

Collection 개체에서 요소를 제거합니다.

Remove(String)

Collection 개체에서 요소를 제거합니다.

ToString()

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

(다음에서 상속됨 Object)

명시적 인터페이스 구현

ICollection.CopyTo(Array, Int32)

특정 Collection 인덱스부터 시작하여 Array의 요소를 Array에 복사합니다.

ICollection.Count

이 컬렉션의 항목 수를 가져옵니다.

ICollection.IsSynchronized

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

ICollection.SyncRoot

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

IDeserializationCallback.OnDeserialization(Object)

전체 Collection 개체 그래프가 역직렬화된 후 실행됩니다.

IEnumerable.GetEnumerator()

컬렉션을 반복하는 열거자를 가져옵니다.

IList.Add(Object)

컬렉션에 항목을 추가합니다.

IList.Clear()

Collection 개체에서 항목을 모두 제거합니다.

IList.Contains(Object)

Collection 개체에 특정 값이 들어 있는지 여부를 확인합니다.

IList.IndexOf(Object)

Collection 개체에서 특정 항목의 인덱스를 확인합니다.

IList.Insert(Int32, Object)

Collection 개체의 지정한 인덱스에 항목을 삽입합니다.

IList.IsFixedSize

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

IList.IsReadOnly

Collection 개체가 있는지 여부를 나타내는 값을 가져옵니다.

IList.Item[Int32]

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

IList.Remove(Object)

Collection 개체에서 특정 개체가 처음 나타나는 부분을 제거합니다.

IList.RemoveAt(Int32)

지정한 인덱스에 있는 Collection 개체 항목을 제거합니다.

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

Collection 개체를 serialize하는 데 필요한 데이터를 반환합니다.

확장 메서드

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable)

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

AsQueryable(IEnumerable)

IEnumerableIQueryable로 변환합니다.

적용 대상