C# | Get an ICollection containing the values in HybridDictionary

HybridDictionary.Values property is used to get an ICollection containing the values in the HybridDictionary.

Syntax:

public System.Collections.ICollection Values { get; }

Return Value: It returns an ICollection containing the values in the HybridDictionary.

Below programs illustrate the use of HybridDictionary.Values property:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to get an ICollection containing
// the values in the HybridDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HybridDictionary named myDict
        HybridDictionary myDict = new HybridDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("A", "Apple");
        myDict.Add("B", "Banana");
        myDict.Add("C", "Cat");
        myDict.Add("D", "Dog");
        myDict.Add("E", "Elephant");
        myDict.Add("F", "Fish");
  
        // Creating a String arr named myArr
        String[] myArr = new String[myDict.Count];
  
        // copying the Values in HybridDictionary
        // to a one-dimensional Array instance
        // at the specified index.
        myDict.Values.CopyTo(myArr, 0);
  
        // To get an ICollection containing
        // the Values in the HybridDictionary
        for (int i = 0; i < myDict.Count; i++)
            Console.WriteLine(myArr[i]);
    }
}

chevron_right


Output:

Apple
Banana
Cat
Dog
Elephant
Fish

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to get an ICollection containing
// the values in the HybridDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HybridDictionary named myDict
        HybridDictionary myDict = new HybridDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("I", "first");
        myDict.Add("II", "second");
        myDict.Add("III", "third");
        myDict.Add("IV", "fourth");
        myDict.Add("V", "fifth");
  
        // Creating a String arr named myArr
        String[] myArr = new String[myDict.Count];
  
        // copying the Values in HybridDictionary
        // to a one-dimensional Array instance
        // at the specified index.
        myDict.Values.CopyTo(myArr, 0);
  
        // To get an ICollection containing
        // the Values in the HybridDictionary
        for (int i = 0; i < myDict.Count; i++)
            Console.WriteLine(myArr[i]);
    }
}

chevron_right


Output:

first
second
third
fourth
fifth

Note:

  • The order of the values in the ICollection is unspecified, but it is the same order as the associated keys in the ICollection returned by the Keys method.
  • The returned ICollection is not a static copy. Instead, the ICollection refers back to the values in the original HybridDictionary. Therefore, changes to the HybridDictionary continue to be reflected in the ICollection.
  • Retrieving the value of this property is an O(1) operation.

Reference:



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.