Collection<T> Collection<T> Collection<T> Collection<T> Constructors

정의

오버로드

Collection<T>() Collection<T>() Collection<T>()

비어 있는 Collection<T> 클래스의 새 인스턴스를 초기화합니다.Initializes a new instance of the Collection<T> class that is empty.

Collection<T>(IList<T>) Collection<T>(IList<T>) Collection<T>(IList<T>) Collection<T>(IList<T>)

Collection<T> 클래스의 새 인스턴스를 지정된 목록의 래퍼로 초기화합니다.Initializes a new instance of the Collection<T> class as a wrapper for the specified list.

Collection<T>() Collection<T>() Collection<T>()

비어 있는 Collection<T> 클래스의 새 인스턴스를 초기화합니다.Initializes a new instance of the Collection<T> class that is empty.

public:
 Collection();
public Collection ();
Public Sub New ()

예제

다음 코드 예제 대부분의 속성 및 메서드의 Collection<T>합니다.The following code example demonstrates many of the properties and methods of Collection<T>. 코드 예제에서는 문자열의 컬렉션을 만듭니다는 Collection<T>() 생성자를 사용 하 여는 Add 여러 문자열을 추가 하는 메서드를 표시 합니다 Count, 문자열을 나열 하 고 합니다.The code example creates a collection of strings with the Collection<T>() constructor, uses the Add method to add several strings, displays the Count, and lists the strings. 이 예제에서는 사용 합니다 IndexOf 메서드는 문자열의 인덱스를 및 Contains 문자열 컬렉션에 있는지 확인 하는 방법입니다.The example uses the IndexOf method to find the index of a string and the Contains method to determine whether a string is in the collection. 사용 하 여 문자열을 삽입 하는 예제는 Insert 기본값을 사용 하는 메서드 및 검색 설정 문자열 Item[Int32] 속성 (C# 인덱서).The example inserts a string using the Insert method and retrieves and sets strings using the default Item[Int32] property (the indexer in C#). 예제에서는 문자열 identity를 사용 하 여 문자열을 제거 합니다 Remove 메서드 및 인덱스를 사용 하 여는 RemoveAt 메서드.The example removes strings by string identity using the Remove method and by index using the RemoveAt method. 마지막으로 Clear 메서드는 컬렉션에서 모든 문자열을 지우는 데 사용 됩니다.Finally, the Clear method is used to clear all strings from the collection.

using namespace System;
using namespace System::Collections::Generic;
using namespace System::Collections::ObjectModel;

public ref class Demo
{
public:
    static void Main()
    {
        Collection<String^>^ dinosaurs = gcnew Collection<String^>();

        dinosaurs->Add("Psitticosaurus");
        dinosaurs->Add("Caudipteryx");
        dinosaurs->Add("Compsognathus");
        dinosaurs->Add("Muttaburrasaurus");

        Console::WriteLine("{0} dinosaurs:", dinosaurs->Count);
        Display(dinosaurs);

        Console::WriteLine("\nIndexOf(\"Muttaburrasaurus\"): {0}",
            dinosaurs->IndexOf("Muttaburrasaurus"));

        Console::WriteLine("\nContains(\"Caudipteryx\"): {0}",
            dinosaurs->Contains("Caudipteryx"));

        Console::WriteLine("\nInsert(2, \"Nanotyrannus\")");
        dinosaurs->Insert(2, "Nanotyrannus");
        Display(dinosaurs);

        Console::WriteLine("\ndinosaurs[2]: {0}", dinosaurs[2]);

        Console::WriteLine("\ndinosaurs[2] = \"Microraptor\"");
        dinosaurs[2] = "Microraptor";
        Display(dinosaurs);

        Console::WriteLine("\nRemove(\"Microraptor\")");
        dinosaurs->Remove("Microraptor");
        Display(dinosaurs);

        Console::WriteLine("\nRemoveAt(0)");
        dinosaurs->RemoveAt(0);
        Display(dinosaurs);

        Console::WriteLine("\ndinosaurs.Clear()");
        dinosaurs->Clear();
        Console::WriteLine("Count: {0}", dinosaurs->Count);
    }

private:
    static void Display(Collection<String^>^ cs)
    {
        Console::WriteLine();
        for each( String^ item in cs )
        {
            Console::WriteLine(item);
        }
    }
};

int main()
{
    Demo::Main();
}

/* This code example produces the following output:

4 dinosaurs:

Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus

IndexOf("Muttaburrasaurus"): 3

Contains("Caudipteryx"): True

Insert(2, "Nanotyrannus")

Psitticosaurus
Caudipteryx
Nanotyrannus
Compsognathus
Muttaburrasaurus

dinosaurs[2]: Nanotyrannus

dinosaurs[2] = "Microraptor"

Psitticosaurus
Caudipteryx
Microraptor
Compsognathus
Muttaburrasaurus

Remove("Microraptor")

Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus

RemoveAt(0)

Caudipteryx
Compsognathus
Muttaburrasaurus

dinosaurs.Clear()
Count: 0
 */
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

public class Demo
{
    public static void Main()
    {
        Collection<string> dinosaurs = new Collection<string>();

        dinosaurs.Add("Psitticosaurus");
        dinosaurs.Add("Caudipteryx");
        dinosaurs.Add("Compsognathus");
        dinosaurs.Add("Muttaburrasaurus");

        Console.WriteLine("{0} dinosaurs:", dinosaurs.Count);
        Display(dinosaurs);
    
        Console.WriteLine("\nIndexOf(\"Muttaburrasaurus\"): {0}", 
            dinosaurs.IndexOf("Muttaburrasaurus"));

        Console.WriteLine("\nContains(\"Caudipteryx\"): {0}", 
            dinosaurs.Contains("Caudipteryx"));

        Console.WriteLine("\nInsert(2, \"Nanotyrannus\")");
        dinosaurs.Insert(2, "Nanotyrannus");
        Display(dinosaurs);

        Console.WriteLine("\ndinosaurs[2]: {0}", dinosaurs[2]);

        Console.WriteLine("\ndinosaurs[2] = \"Microraptor\"");
        dinosaurs[2] = "Microraptor";
        Display(dinosaurs);

        Console.WriteLine("\nRemove(\"Microraptor\")");
        dinosaurs.Remove("Microraptor");
        Display(dinosaurs);

        Console.WriteLine("\nRemoveAt(0)");
        dinosaurs.RemoveAt(0);
        Display(dinosaurs);

        Console.WriteLine("\ndinosaurs.Clear()");
        dinosaurs.Clear();
        Console.WriteLine("Count: {0}", dinosaurs.Count);
    }
    
    private static void Display(Collection<string> cs)
    {
        Console.WriteLine();
        foreach( string item in cs )
        {
            Console.WriteLine(item);
        }
    }
}

/* This code example produces the following output:

4 dinosaurs:

Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus

IndexOf("Muttaburrasaurus"): 3

Contains("Caudipteryx"): True

Insert(2, "Nanotyrannus")

Psitticosaurus
Caudipteryx
Nanotyrannus
Compsognathus
Muttaburrasaurus

dinosaurs[2]: Nanotyrannus

dinosaurs[2] = "Microraptor"

Psitticosaurus
Caudipteryx
Microraptor
Compsognathus
Muttaburrasaurus

Remove("Microraptor")

Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus

RemoveAt(0)

Caudipteryx
Compsognathus
Muttaburrasaurus

dinosaurs.Clear()
Count: 0
 */
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Public Class Demo
    
    Public Shared Sub Main() 

        Dim dinosaurs As New Collection(Of String)

        dinosaurs.Add("Psitticosaurus")
        dinosaurs.Add("Caudipteryx")
        dinosaurs.Add("Compsognathus")
        dinosaurs.Add("Muttaburrasaurus")

        Console.WriteLine("{0} dinosaurs:", dinosaurs.Count)
        Display(dinosaurs)
    
        Console.WriteLine(vbLf & "IndexOf(""Muttaburrasaurus""): {0}", _
            dinosaurs.IndexOf("Muttaburrasaurus"))

        Console.WriteLine(vbLf & "Contains(""Caudipteryx""): {0}", _
            dinosaurs.Contains("Caudipteryx"))

        Console.WriteLine(vbLf & "Insert(2, ""Nanotyrannus"")")
        dinosaurs.Insert(2, "Nanotyrannus")
        Display(dinosaurs)

        Console.WriteLine(vbLf & "dinosaurs(2): {0}", dinosaurs(2))

        Console.WriteLine(vbLf & "dinosaurs(2) = ""Microraptor""")
        dinosaurs(2) = "Microraptor"
        Display(dinosaurs)

        Console.WriteLine(vbLf & "Remove(""Microraptor"")")
        dinosaurs.Remove("Microraptor")
        Display(dinosaurs)

        Console.WriteLine(vbLf & "RemoveAt(0)")
        dinosaurs.RemoveAt(0)
        Display(dinosaurs)
 
        Console.WriteLine(vbLf & "dinosaurs.Clear()")
        dinosaurs.Clear()
        Console.WriteLine("Count: {0}", dinosaurs.Count)

    End Sub
    
    Private Shared Sub Display(ByVal cs As Collection(Of String)) 
        Console.WriteLine()
        For Each item As String In cs
            Console.WriteLine(item)
        Next item
    End Sub
End Class

' This code example produces the following output:
'
'4 dinosaurs:
'
'Psitticosaurus
'Caudipteryx
'Compsognathus
'Muttaburrasaurus
'
'IndexOf("Muttaburrasaurus"): 3
'
'Contains("Caudipteryx"): True
'
'Insert(2, "Nanotyrannus")
'
'Psitticosaurus
'Caudipteryx
'Nanotyrannus
'Compsognathus
'Muttaburrasaurus
'
'dinosaurs(2): Nanotyrannus
'
'dinosaurs(2) = "Microraptor"
'
'Psitticosaurus
'Caudipteryx
'Microraptor
'Compsognathus
'Muttaburrasaurus
'
'Remove("Microraptor")
'
'Psitticosaurus
'Caudipteryx
'Compsognathus
'Muttaburrasaurus
'
'RemoveAt(0)
'
'Caudipteryx
'Compsognathus
'Muttaburrasaurus
'
'dinosaurs.Clear()
'Count: 0

설명

이 생성자는 o (1) 작업입니다.This constructor is an O(1) operation.

Collection<T>(IList<T>) Collection<T>(IList<T>) Collection<T>(IList<T>) Collection<T>(IList<T>)

Collection<T> 클래스의 새 인스턴스를 지정된 목록의 래퍼로 초기화합니다.Initializes a new instance of the Collection<T> class as a wrapper for the specified list.

public:
 Collection(System::Collections::Generic::IList<T> ^ list);
public Collection (System.Collections.Generic.IList<T> list);
new System.Collections.ObjectModel.Collection<'T> : System.Collections.Generic.IList<'T> -> System.Collections.ObjectModel.Collection<'T>
Public Sub New (list As IList(Of T))

매개 변수

list
IList<T> IList<T> IList<T> IList<T>

새 컬렉션이 래핑하는 목록입니다.The list that is wrapped by the new collection.

예외

설명

목록 요소의 복사 되지 않습니다.The elements of the list are not copied. 목록 요소의 후속 변경 내용을 통해 표시 되도록 목록 컬렉션에 의해 래핑되는 Collection<T>합니다.The list is wrapped by the collection, so that subsequent changes to the elements of the list are visible through the Collection<T>.

이 생성자는 o (1) 작업입니다.This constructor is an O(1) operation.

적용 대상