HashSet<T>.Contains(T) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Determines whether a HashSet<T> object contains the specified element.
public:
virtual bool Contains(T item);
public bool Contains(T item);
abstract member Contains : 'T -> bool
override this.Contains : 'T -> bool
Public Function Contains (item As T) As Boolean
Parameters
- item
- T
The element to locate in the HashSet<T> object.
Returns
true
if the HashSet<T> object contains the specified element; otherwise, false
.
Implements
Examples
The following example demonstrates how to remove values from a HashSet<T> collection using the Remove method. In this example, the Contains method verifies that the set contains a value before removing it.
HashSet<int> numbers = new HashSet<int>();
for (int i = 0; i < 20; i++) {
numbers.Add(i);
}
// Display all the numbers in the hash table.
Console.Write("numbers contains {0} elements: ", numbers.Count);
DisplaySet(numbers);
// Remove all odd numbers.
numbers.RemoveWhere(IsOdd);
Console.Write("numbers contains {0} elements: ", numbers.Count);
DisplaySet(numbers);
// Check if the hash table contains 0 and, if so, remove it.
if (numbers.Contains(0)) {
numbers.Remove(0);
}
Console.Write("numbers contains {0} elements: ", numbers.Count);
DisplaySet(numbers);
bool IsOdd(int i)
{
return ((i % 2) == 1);
}
void DisplaySet(HashSet<int> set)
{
Console.Write("{");
foreach (int i in set)
Console.Write(" {0}", i);
Console.WriteLine(" }");
}
// This example displays the following output:
// numbers contains 20 elements: { 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 }
// numbers contains 10 elements: { 0 2 4 6 8 10 12 14 16 18 }
// numbers contains 9 elements: { 2 4 6 8 10 12 14 16 18 }
let isOdd i = i % 2 = 1
let displaySet (set: HashSet<int>) =
printf "{"
for i in set do
printf $" {i}"
printfn " }"
let numbers = HashSet<int>()
for i in 1..20 do
numbers.Add i |> ignore
// Display all the numbers in the hash table.
printf $"numbers contains {numbers.Count} elements: "
displaySet numbers
// Remove all odd numbers.
numbers.RemoveWhere isOdd |> ignore
printf $"numbers contains {numbers.Count} elements: "
displaySet numbers
// Check if the hash table contains 0 and, if so, remove it.
if numbers.Contains 0 then
numbers.Remove 0 |> ignore
printf $"numbers contains {numbers.Count} elements: "
displaySet numbers
// This example displays the following output:
// numbers contains 20 elements: { 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 }
// numbers contains 10 elements: { 0 2 4 6 8 10 12 14 16 18 }
// numbers contains 9 elements: { 2 4 6 8 10 12 14 16 18 }
Imports System.Collections.Generic
Module Example
Public Sub Main()
Dim numbers As New HashSet(Of Integer)()
For i As Integer = 0 To 19
numbers.Add(i)
Next i
' Display all the numbers in the hash table.
Console.Write("numbers contains {0} elements: ", numbers.Count)
DisplaySet(numbers)
' Remove all odd numbers.
numbers.RemoveWhere(AddressOf IsOdd)
Console.Write("numbers contains {0} elements: ", numbers.Count)
DisplaySet(numbers)
' Check if the hash table contains 0 and, if so, remove it.
If numbers.Contains(0) Then
numbers.Remove(0)
End If
Console.Write("numbers contains {0} elements: ", numbers.Count)
DisplaySet(numbers)
End Sub
Private Function IsOdd(ByVal i As Integer) As Boolean
Return ((i Mod 2) = 1)
End Function
Private Sub DisplaySet(ByVal coll As HashSet(Of Integer))
Console.Write("{")
For Each i As Integer In coll
Console.Write(" {0}", i)
Next
Console.WriteLine(" }")
End Sub
End Module
' The example displays the following output:
' numbers contains 20 elements: { 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 }
' numbers contains 10 elements: { 0 2 4 6 8 10 12 14 16 18 }
' numbers contains 9 elements: { 2 4 6 8 10 12 14 16 18 }
Remarks
This method is an O(1) operation.
Applies to
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET