c# vb.net Finds whether a certain key name HashSet, Collection, Dictionary object usage exists

  • How to set vb.net Collection object to ignore case and how to detect whether it contains an item
  • In VB.NET, you can use StringComparerclasses to set Collectionignore case for objects. StringComparerThe class provides several static properties that can be used to create case-insensitive comparators. where StringComparer.OrdinalIgnoreCasethe property creates a case-insensitive comparator.

    Here is a sample code that demonstrates how to set Collectionignore case for an object:

    复制Dim myCollection As New Collection(StringComparer.OrdinalIgnoreCase)
    
    myCollection.Add("Apple")
    myCollection.Add("Banana")
    myCollection.Add("Orange")
    
    ' 检测是否含有某项
    If myCollection.Contains("apple") Then
        Console.WriteLine("包含 'apple'")
    Else
        Console.WriteLine("不包含 'apple'")
    End If
    

    The output will be "contains 'apple'" because Containsthe method ignores case when comparing.

    In addition, Collectionthe object also provides Containsmethods to detect whether it contains an item. This method accepts a parameter representing the item to detect. Returns if Collectionthe object contains the item, Trueotherwise False.

    Hope this helps!

  • Unrepeated keys do not require values. What object should be used? You can detect whether it contains a certain KEY from the object.
  • If you only need unique keys and not corresponding values, you can use HashSet(Of TKey)objects. HashSet(Of TKey)Is a collection class that stores unique elements and provides efficient search operations.

    Here is a sample code that demonstrates how to use HashSet(Of TKey)an object to detect whether a key is contained:

    复制Dim myHashSet As New HashSet(Of String)
    
    myHashSet.Add("Apple")
    myHashSet.Add("Banana")
    myHashSet.Add("Orange")
    
    ' 检测是否含有某个键
    If myHashSet.Contains("Apple") Then
        Console.WriteLine("包含 'Apple'")
    Else
        Console.WriteLine("不包含 'Apple'")
    End If
    

    The output will be "contains 'Apple'" because Containsthe method can be used to detect HashSet(Of TKey)whether the object contains a certain key.

    Hope this helps!

Guess you like

Origin blog.csdn.net/xiaoyao961/article/details/135419553