ReadOnlyCollection<T>.Contains(T) Methode

Definition

Bestimmt, ob sich ein Element in ReadOnlyCollection<T> befindet.

public:
 virtual bool Contains(T value);
public bool Contains (T value);
abstract member Contains : 'T -> bool
override this.Contains : 'T -> bool
Public Function Contains (value As T) As Boolean

Parameter

value
T

Das im ReadOnlyCollection<T> zu suchende Objekt. Der Wert kann für Verweistypen null sein.

Gibt zurück

true, wenn das value in der ReadOnlyCollection<T> gefunden wird, andernfalls false.

Implementiert

Beispiele

Im folgenden Codebeispiel werden mehrere Member der ReadOnlyCollection<T> -Klasse veranschaulicht. Im Codebeispiel wird eine List<T> von Zeichenfolgen erstellt und vier Dinosauriernamen hinzugefügt. Das Codebeispiel umschließt die Liste dann in einen ReadOnlyCollection<T>.

Nach dem Demonstrieren der CountMember , Contains, Item[]und IList.IndexOf zeigt das Codebeispiel, dass es ReadOnlyCollection<T> sich nur um einen Wrapper für das Original List<T> handelt, indem ein neues Element hinzugefügt List<T> und der Inhalt des ReadOnlyCollection<T>angezeigt wird.

Schließlich erstellt das Codebeispiel ein Array, das größer als die Auflistung ist, und verwendet die CopyTo -Methode, um die Elemente der Auflistung in die Mitte des Arrays einzufügen.

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

void main()
{
    List<String^>^ dinosaurs = gcnew List<String^>();

    dinosaurs->Add("Tyrannosaurus");
    dinosaurs->Add("Amargasaurus");
    dinosaurs->Add("Deinonychus");
    dinosaurs->Add("Compsognathus");

    ReadOnlyCollection<String^>^ readOnlyDinosaurs = 
        gcnew ReadOnlyCollection<String^>(dinosaurs);

    Console::WriteLine();
    for each(String^ dinosaur in readOnlyDinosaurs )
    {
        Console::WriteLine(dinosaur);
    }

    Console::WriteLine("\nCount: {0}", readOnlyDinosaurs->Count);

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

    Console::WriteLine("\nreadOnlyDinosaurs[3]: {0}", 
        readOnlyDinosaurs[3]);

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

    Console::WriteLine("\nInsert into the wrapped List:");
    Console::WriteLine("Insert(2, \"Oviraptor\")");
    dinosaurs->Insert(2, "Oviraptor");

    Console::WriteLine();
    for each( String^ dinosaur in readOnlyDinosaurs )
    {
        Console::WriteLine(dinosaur);
    }

    array<String^>^ dinoArray = 
        gcnew array<String^>(readOnlyDinosaurs->Count + 2);
    readOnlyDinosaurs->CopyTo(dinoArray, 1);

    Console::WriteLine("\nCopied array has {0} elements:", 
        dinoArray->Length);
    for each( String^ dinosaur in dinoArray )
    {
        Console::WriteLine("\"{0}\"", dinosaur);
    }
}

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Deinonychus
Compsognathus

Count: 4

Contains("Deinonychus"): True

readOnlyDinosaurs[3]: Compsognathus

IndexOf("Compsognathus"): 3

Insert into the wrapped List:
Insert(2, "Oviraptor")

Tyrannosaurus
Amargasaurus
Oviraptor
Deinonychus
Compsognathus

Copied array has 7 elements:
""
"Tyrannosaurus"
"Amargasaurus"
"Oviraptor"
"Deinonychus"
"Compsognathus"
""
 */
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

public class Example
{
    public static void Main()
    {
        List<string> dinosaurs = new List<string>();

        dinosaurs.Add("Tyrannosaurus");
        dinosaurs.Add("Amargasaurus");
        dinosaurs.Add("Deinonychus");
        dinosaurs.Add("Compsognathus");

        ReadOnlyCollection<string> readOnlyDinosaurs =
            new ReadOnlyCollection<string>(dinosaurs);

        Console.WriteLine();
        foreach( string dinosaur in readOnlyDinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nCount: {0}", readOnlyDinosaurs.Count);

        Console.WriteLine("\nContains(\"Deinonychus\"): {0}",
            readOnlyDinosaurs.Contains("Deinonychus"));

        Console.WriteLine("\nreadOnlyDinosaurs[3]: {0}",
            readOnlyDinosaurs[3]);

        Console.WriteLine("\nIndexOf(\"Compsognathus\"): {0}",
            readOnlyDinosaurs.IndexOf("Compsognathus"));

        Console.WriteLine("\nInsert into the wrapped List:");
        Console.WriteLine("Insert(2, \"Oviraptor\")");
        dinosaurs.Insert(2, "Oviraptor");

        Console.WriteLine();
        foreach( string dinosaur in readOnlyDinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        string[] dinoArray = new string[readOnlyDinosaurs.Count + 2];
        readOnlyDinosaurs.CopyTo(dinoArray, 1);

        Console.WriteLine("\nCopied array has {0} elements:",
            dinoArray.Length);
        foreach( string dinosaur in dinoArray )
        {
            Console.WriteLine("\"{0}\"", dinosaur);
        }
    }
}

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Deinonychus
Compsognathus

Count: 4

Contains("Deinonychus"): True

readOnlyDinosaurs[3]: Compsognathus

IndexOf("Compsognathus"): 3

Insert into the wrapped List:
Insert(2, "Oviraptor")

Tyrannosaurus
Amargasaurus
Oviraptor
Deinonychus
Compsognathus

Copied array has 7 elements:
""
"Tyrannosaurus"
"Amargasaurus"
"Oviraptor"
"Deinonychus"
"Compsognathus"
""
 */
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Public Class Example

    Public Shared Sub Main()

        Dim dinosaurs As New List(Of String)

        dinosaurs.Add("Tyrannosaurus")
        dinosaurs.Add("Amargasaurus")
        dinosaurs.Add("Deinonychus")
        dinosaurs.Add("Compsognathus")

        Dim readOnlyDinosaurs As _
            New ReadOnlyCollection(Of String)(dinosaurs)

        Console.WriteLine()
        For Each dinosaur As String In readOnlyDinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & "Count: {0}", _
            readOnlyDinosaurs.Count)

        Console.WriteLine(vbLf & "Contains(""Deinonychus""): {0}", _
            readOnlyDinosaurs.Contains("Deinonychus"))

        Console.WriteLine(vbLf & _
            "readOnlyDinosaurs(3): {0}", readOnlyDinosaurs(3))

        Console.WriteLine(vbLf & "IndexOf(""Compsognathus""): {0}", _
            readOnlyDinosaurs.IndexOf("Compsognathus"))

        Console.WriteLine(vbLf & "Insert into the wrapped List:")
        Console.WriteLine("Insert(2, ""Oviraptor"")")
        dinosaurs.Insert(2, "Oviraptor")

        Console.WriteLine()
        For Each dinosaur As String In readOnlyDinosaurs
            Console.WriteLine(dinosaur)
        Next

        Dim dinoArray(readOnlyDinosaurs.Count + 1) As String
        readOnlyDinosaurs.CopyTo(dinoArray, 1)

        Console.WriteLine(vbLf & "Copied array has {0} elements:", _
            dinoArray.Length)
        For Each dinosaur As String In dinoArray
            Console.WriteLine("""{0}""", dinosaur)
        Next

   End Sub
End Class

' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Deinonychus
'Compsognathus
'
'Count: 4
'
'Contains("Deinonychus"): True
'
'readOnlyDinosaurs(3): Compsognathus
'
'IndexOf("Compsognathus"): 3
'
'Insert into the wrapped List:
'Insert(2, "Oviraptor")
'
'Tyrannosaurus
'Amargasaurus
'Oviraptor
'Deinonychus
'Compsognathus
'
'Copied array has 7 elements:
'""
'"Tyrannosaurus"
'"Amargasaurus"
'"Oviraptor"
'"Deinonychus"
'"Compsognathus"
'""

Hinweise

Diese Methode bestimmt die Gleichheit mithilfe des Standardmäßigen Gleichheitsvergleichs EqualityComparer<T>.Default.

Diese Methode führt eine lineare Suche durch. daher ist diese Methode ein O(n)-Vorgang, wobei n ist Count.

Gilt für:

Weitere Informationen