Skip to content
Related Articles

Related Articles

C# Program to Demonstrate the IDictionary Interface

Improve Article
Save Article
  • Last Updated : 05 Jan, 2022
Improve Article
Save Article

IDictionary Interface is an interface that belongs to the collection module where we can access the elements by keys. Or we can say that the IDictionary interface is a collection of key/value pairs. It is available for both generic and non-generic types collection. Here each pair must contain a unique key and the value does not have to be null or can be null. The key/value pairs of non-generic IDictionary Interface are stored in a DictionaryEntery object and the key/value pairs of generic IDictionary Interface are stored in KeyValuePain<TKey, TValue> object.

Syntax:

public interface IDictionary : ICollection, IEnumerable

Or we can use the IDictionary interface by using the following syntax.

void Interface_name(IDictionary<key_datatype, value_datatype> object)
{
    // Methods............
    // Statements.........
}

where key_datatype represents the key datatype and value_datatype represents the value datatype

Property

Let us discuss the common properties of the IDictionary Interface:

  • IsFixedSize: This property gets a value indicating whether the IDictionary object has a fixed size.
  • IsReadOnly: This property gets a value indicating whether the IDictionary object is read-only.
  • Item: This property gets or sets the element with the specified key.
  • Keys: This property gets an ICollection object containing the keys of the IDictionary object.
  • Values: This property gets an ICollection object containing the values in the IDictionary object.
  • Count: This property is used to count the total number of elements present in ICollection.
  • IsSynchronized: This property is used to check whether the access to the ICollection is synchronized.

Method

Let us discuss the common methods of the IDictionary Interface:

  • Add: This method will add an element with the provided key and value to the IDictionary object.
  • Clear: This method will remove all the elements from the IDictionary object.
  • Contains: This method will check whether the IDictionary object contains an element with the specified key or not.
  • CopyTo: This method is used to copy the elements of ICollection to an array, starting from the given array index.
  • GetEnumerator: This method will return an IDictionaryEnumerator object for the IDictionary object.
  • Remove: This method will remove an element with the specified key.

In this article, we are going to create a dictionary and display the data using the IDictionary interface.

Approach

1. Create a dictionary with key and values as string type to store the data of students.

Dictionary<string, string> Student = new Dictionary<string, string>();

2. Add values to dictionary.

Student["Subject"] = "php";
Student["Subject"] = "java";

3. Create a method with IDictionary interface to display the data present in the dictionary.

static void Display(IDictionary<string, string> i)
{
    Console.WriteLine(i["Subject"]);
}

4. Call this method in the main method.

Display(Student);

Example:

C#




// C# program to display IDictionary Interface
using System;
using System.Collections.Generic;
 
class GFG{
 
// Display method with IDictionary interface
static void Display(IDictionary<string, string> i)
{
    Console.WriteLine(i["Subject"]);
}
 
// Driver code
static void Main()
{
     
    // Create a dictionary with student subjects
    Dictionary<string,
               string> Student = new Dictionary<string,
                                                string>();
                                                 
    // Assign the value
    Student["Subject"] = "php";
     
    // Call Display method
    Display(Student);
 
    // Assign the value
    Student["Subject"] = "java";
     
    // Call Display method
    Display(Student);
}
}


 Output:

php
java 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!