Home  >  Article  >  Backend Development  >  Mixed dictionary classes in C#?

Mixed dictionary classes in C#?

王林
王林forward
2023-09-08 11:57:09682browse

C# 中的混合字典类?

The HybridDictionary class implements IDictionary by using ListDictionary when the collection is small, and then switches to Hashtable when the collection becomes larger.

The following are the attributes of the HybridDictionary class:

Serial number Attributes and description
1 Count

Get the number of key/value pairs contained

Mixed dictionary.

2 IsFixedSize

Gets a value indicating whether HybridDictionary has a fixed size.

3 IsReadOnly

Gets a value indicating whether the HybridDictionary is Read only.

4 IsSynchronized

Gets a value indicating whether the HybridDictionary is synchronized (thread safe).

5 #Item[Object]

Gets or sets the value associated with the specified key .

6 Keys

Get the ICollection containing the keys HybridDictionary.

7 SyncRoot

Get objects available for synchronous access to HybridDictionary.

8 Values

Get an ICollection of values ​​contained in a HybridDictionary the HybridDictionary.

The following are some methods of the HybridDictionary class:

##1234567##8GetType() To calculate the number of key-value pairs in HybridDictionary, the code is as follows −
number Method and Description
Add(Object, Object) Adds an entry with the specified key and value to the HybridDictionary.

Clear()Remove all entries from the HybridDictionary .

Contains(Object)Determines whether the HybridDictionary contains a specific key.

CopyTo(Array, Int32)Copy the entries of the HybridDictionary to a in a one-dimensional array The array instance at the specified index.

Equals(Object)Determines whether the specified object is equal to current object. (Inherited from Object)

GetEnumerator()Returns an IDictionaryEnumerator, used To traverse HybridDictionary.

GetHashCode() strong> Used as the default hash function. (Inherited from Object)

Get the Type of the current instance . (Inherited from Object)

Example

Now let us see some examples −

Demonstration

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      HybridDictionary dict1 = new HybridDictionary();
      dict1.Add("A", "SUV");
      dict1.Add("B", "MUV");
      dict1.Add("C", "AUV");
      Console.WriteLine("HybridDictionary1 elements...");
      foreach(DictionaryEntry d in dict1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of Key/value pairs in Dictionary1 = "+dict1.Count);
      HybridDictionary dict2 = new HybridDictionary();
      dict2.Add("1", "One");
      dict2.Add("2", "Two");
      dict2.Add("3", "Three");
      dict2.Add("4", "Four");
      dict2.Add("5", "Five");
      dict2.Add("6", "Six");
      Console.WriteLine("HybridDictionary2 elements...");
      foreach(DictionaryEntry d in dict2) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of Key/value pairs in Dictionary2 = "+dict1.Count);
      dict2.Clear();
      Console.WriteLine("Count of Key/value pairs in Dictionary2 (Updated) = "+dict2.Count);
   }
}

Output

This will produce the following output−

HybridDictionary1 elements...
A SUV
B MUV
C AUV
Count of Key/value pairs in Dictionary1 = 3
HybridDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Count of Key/value pairs in Dictionary2 = 3
Count of Key/value pairs in Dictionary2 (Updated) = 0

To check if the HybridDictionary is synchronized, The code is as follows −

Example

Real-time demonstration

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      HybridDictionary dict1 = new HybridDictionary();
      dict1.Add("A", "Books");
      dict1.Add("B", "Electronics");
      dict1.Add("C", "Smart Wearables");
      dict1.Add("D", "Pet Supplies");
      dict1.Add("E", "Clothing");
      dict1.Add("F", "Footwear");
      Console.WriteLine("HybridDictionary1 elements...");
      foreach(DictionaryEntry d in dict1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Is the HybridDictionary1 having fixed size? = "+dict1.IsFixedSize);
      Console.WriteLine("If HybridDictionary1 read-only? = "+dict1.IsReadOnly);
      Console.WriteLine("Is HybridDictionary1 synchronized = "+dict1.IsSynchronized);
      HybridDictionary dict2 = new HybridDictionary();
      dict2.Add("1", "One");
      dict2.Add("2", "Two");
      dict2.Add("3", "Three");
      dict2.Add("4", "Four");
      dict2.Add("5", "Five");
      dict2.Add("6", "Six");
      Console.WriteLine("HybridDictionary2 elements...");
      foreach(DictionaryEntry d in dict2) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Is HybridDictionary1 equal to HybridDictionary2? = "+(dict1.Equals(dict2)));
      Console.WriteLine("Is the HybridDictionary2 having fixed size? = "+dict2.IsFixedSize);
      Console.WriteLine("If HybridDictionary2 read-only? = "+dict2.IsReadOnly);
      Console.WriteLine("Is HybridDictionary2 synchronized = "+dict2.IsSynchronized);
   }
}

Output

This will produce the following output−

HybridDictionary1 elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Is the HybridDictionary1 having fixed size? = False
If HybridDictionary1 read-only? = False
Is HybridDictionary1 synchronized = False
HybridDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Is HybridDictionary1 equal to HybridDictionary2? = False
Is the HybridDictionary2 having fixed size? = False
If HybridDictionary2 read-only? = False
Is HybridDictionary2 synchronized = False

The above is the detailed content of Mixed dictionary classes in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete