Collection.Contains(String) 方法


返回一个值,该值指示 Visual Basic Collection 对象是否包含一个带有特定键的元素。Returns a value that indicates whether a Visual Basic Collection object contains an element with a specific key.

 bool Contains(System::String ^ Key);
public bool Contains (string Key);
member this.Contains : string -> bool
Public Function Contains (Key As String) As Boolean



必需。Required. String 表达式,指定搜索集合的元素所用的键。A String expression that specifies the key for which to search the elements of the collection.



如果 Visual Basic Collection 对象包含一个带有特定键的元素,则为 true;否则为 falsetrue if Visual Basic Collection object contains an element with a specific key; otherwise, false.


Dim customers As New Microsoft.VisualBasic.Collection()
Dim accountNumber As String = "1234"
' Insert code that obtains new customer objects.
' Use the new customer's account number as the key.
customers.Add(newCustomer, accountNumber)
' The preceding statements can be repeated for several customers.
Dim searchNumber As String = "1234"
' Insert code to obtain an account number to search for.
If customers.Contains(searchNumber) Then
    MsgBox("The desired customer is in the collection.")
    MsgBox("The desired customer is not in the collection.")
End If

如果要使用其键搜索元素的集合,请记住在 Key 每次调用方法时提供自变量 AddIf you intend to search the collection for elements using their keys, remember to supply the Key argument every time you call the Add method.


Contains``True如果集合包含具有完全匹配的键的元素,则返回 KeyContains returns True if the collection contains an element with a key exactly matching Key. 否则,Contains 将返回 FalseOtherwise, Contains returns False. 匹配键值时忽略大小写。Case is ignored when matching key values.

Visual Basic Collection 可以保存某些元素,这些元素具有键和其他不含键的元素。A Visual Basic Collection can hold some elements that have keys and other elements without keys. 这取决于对方法的调用是否 Add 向可选参数提供参数 KeyThis depends on whether the call to the Add method supplies an argument to the optional Key parameter.