Javatpoint Logo
Javatpoint Logo

HybridDictionary.Contains(Object) method in C#

In this article, we will discuss the "HybridDictionary.Contains(Object)" method in C# with its syntax, parameters, and examples. But before discussing the "HybridDictionary.Contains(Object)" method, we must know about the HybridDictionary Class in C#.

What is the HybridDictionary Class?

The HybridDictionary class should be used when the amount of elements in a dictionary is uncertain. It uses hash tables and linked lists as data structures. In order to implement IDictionary, it uses a Hashtable for big collections and a ListDictionary for smaller collections.

Properties of the HybridDictionary Class

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

  1. This class is suggested in situations when a dictionary's element count is undetermined.
  2. 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.
  3. A value can be null, but a key cannot be null.

What is the HybridDictionary.Contains(Object) method?

The C# HybridDictionary.Contains(Object) method is used to determine whether a given key is present in the HybridDictionary. It accepts an object parameter representing the key to be checked and returns a boolean value indicating whether the key is present in the dictionary.

Exception: If the key is null, the method raises ArgumentNullException.

Syntax:

It has the following syntax:

a) Modifiers of Visibility

public: It indicates that access to the method is possible from outside the class in which it is declared. It can be called from anywhere.

b) Return Type

bool: It indicates the kind of value the method returns. In this case, the method Contains returns a boolean result (true or false) denoting the existence or absence of the specified key in the collection.

c) Method Name

Contains: It is the method's name. It implies that the method is applied to determine whether a certain key is present in the collection.

d) Parameters

(object key): It indicates the parameter that is accepted by the method. It expects an object parameter called a key, which represents the collection's key that has to be checked. The object type allows for flexibility in the type of key that can be passed to the method.

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 us take an example to illustrate the HybridDictionary.Contains(Object) method in C#.

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 us take another example to illustrate the HybridDictionary.Contains(Object) method in C#.

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.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA