Hybriddictionary.Contains(Object) Method In C#

In this guide, we are going to explore the "HybridDictionary.Contains(Object)" technique in C#, covering its syntax, parameters, and illustrations. Prior to delving into the specifics of the "HybridDictionary.Contains(Object)" method, it is essential to have a clear understanding of the HybridDictionary Class in C#.

What is the HybridDictionary Class?

The HybridDictionary class is suitable for situations where the size of a dictionary is unknown. It employs a combination of hash tables and linked lists for storing data. To support the IDictionary interface, it employs a Hashtable for larger collections and a ListDictionary for smaller collections.

Properties of the HybridDictionary Class

Several properties of the HybridDictionary class in C# are as follows:

  • This class is suggested in situations when a dictionary's element c ount is undetermined.
  • The collection is kept in a hashtable to avoid the overhead of copying elements from the ListDictionary to a hashtable if its initial size exceeds the optimal size for a ListDictionary.
  • A value can be null, but a key cannot be null.
  • What is the HybridDictionary.Contains(Object) method?

The C# HybridDictionary.Contains(Object) function is employed to ascertain the existence of a specific key within the HybridDictionary. It takes in an object parameter that signifies the key to be validated and provides a boolean result denoting the presence of the key in the dictionary.

If the key is null, the method will throw an ArgumentNullException.

Syntax:

It has the following syntax:

Example

public bool Contains (object key);

a) Modifiers of Visibility

The 'public' keyword signifies that the method can be accessed from external classes and can be invoked from any part of the program.

b) Return Type

bool: It signifies the type of data that the function returns. In this instance, the Contains method yields a boolean outcome (either true or false) indicating whether the specified key is present or not within the collection.

c) Method Name

Involves: This represents the name of the method. It indicates that the method is utilized to check if a specific key exists within the collection.

d) Parameters

(object key): This signifies the input accepted by the function. It requires an object parameter named key, which denotes the key of the collection to be verified. The object type provides versatility in the kind of key that can be supplied to the function.

Performance:

  • Contains method performance is dependent upon the number of elements kept in the dictionary.
  • The Contains method offers constant-time performance for hash-based storage because its time complexity is O(1) .
  • The time complexity for list-based storage is O(n) , where 'n' is the number of dictionary elements.
  • Uses of HybridDictionary.Contains Method:

  • In order to verify if a certain key exists before accessing or modifying its associated value, developers frequently utilize the Contains method.
  • This method is helpful for avoiding exceptions that may occur when trying to access a key that doesn't exist in the dictionary.
  • Example 1:

Let's consider a scenario to demonstrate the application of the HybridDictionary.Contains(Object) method in the C# programming language.

Example

using System;
using System.Collections;
using System.Collections.Specialized; 
class C# Tutorial
{
    static void Main(string[] args)
    {
        // Creating a new HybridDictionary named 'my_Dictionary'.
        HybridDictionary my_Dictionary = new HybridDictionary();
        // Add some key-value pairs to the my_Dictionary
        my_Dictionary.Add(1, "Mogili");
        my_Dictionary.Add(2, "Mahesh");
        my_Dictionary.Add(3, "Rajin");
        my_Dictionary.Add(4, "Pavan");
        // Check if the my_Dictionary contains a specific key
        bool containsKey = my_Dictionary.Contains(4);
        foreach(DictionaryEntry q in my_Dictionary) 
        {
            Console.WriteLine(q.Key + " " + q.Value); 
        }
        // Displaying the result
        Console.WriteLine($"HybridDictionary contains key '4': {containsKey}");
        // Check if the my_Dictionary contains a key that doesn't exist
        bool non_Key = my_Dictionary.Contains("Apple");
        // Displaying the result
        Console.WriteLine($"HybridDictionary contains key 'Apple': {non_Key}");
    }
}

Output:

Output

1 Mogili
2 Mahesh
3 Rajin
4 Pavan
HybridDictionary contains key '4': True
HybridDictionary contains key 'Apple': False

Explanation:

  • The code demonstrates how to use a HybridDictionary , a collection that combines the speed and versatility of a hashtable with the flexibility of a list dictionary.
  • It adds the dictionary with key-value pairs and checks keys.
  • It iterates across the dictionary to print every key-value pair.
  • The key-value pairs in the my_Dictionary , and the presence of the key "Apple" (false in this example) and the key 4 (true in this example) in the HybridDictionary are all displayed in the output.
  • Example 2:

Let's consider another scenario to demonstrate the application of the HybridDictionary.Contains(Object) method in the C# programming language.

Example

using System;
using System.Collections;
using System.Collections.Specialized; 
class C# Tutorial
{
    static void Main(string[] args)
    {
        // Creating a new HybridDictionary named 'my_Dictionary'.
        HybridDictionary my_Dictionary = new HybridDictionary();
        // Add some key-value pairs to the my_Dictionary
        my_Dictionary.Add(1, "Mogili");
        my_Dictionary.Add(2, "Mahesh");
        my_Dictionary.Add(3, "Rajin");
        my_Dictionary.Add(4, "Pavan");
        // Check if the my_Dictionary contains a specific key
        bool containsKey = my_Dictionary.Contains(null);
        foreach(DictionaryEntry q in my_Dictionary) 
        {
            Console.WriteLine(q.Key + " " + q.Value); 
        }
        // Displaying the result
        Console.WriteLine($"HybridDictionary contains key '4': {containsKey}");
        // Check if the my_Dictionary contains a key that doesn't exist
        bool non_Key = my_Dictionary.Contains("Apple");
        // Displaying the result
        Console.WriteLine($"HybridDictionary contains key 'Apple': {non_Key}");
    }
}

Output:

Output

Unhandled Exception:
System.ArgumentNullException: Value cannot be null.

Explanation:

  • In this example, the code demonstrates how to store key-value pairs using a HybridDictionary .
  • It shows how to use the Contains method to determine whether the keys "Apple" and "null" exist.
  • After that, the loop iterates across it to output every key-value pair in the dictionary.
  • Due to potential ambiguities and issues, it's uncommon to use "null" because a key in dictionaries with retrieval and raises an exception called "ArgumentNullException" .
  • After that, the presence of a null key in this example might be for demonstration purposes.

Input Required

This code uses input(). Please provide values below: