C# Program to Demonstrate the IDictionary Interface
Last Updated :
27 Feb, 2023
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 DictionaryEntry 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#
using System;
using System.Collections.Generic;
class GFG{
static void Display(IDictionary< string , string > i)
{
Console.WriteLine(i[ "Subject" ]);
}
static void Main()
{
Dictionary< string ,
string > Student = new Dictionary< string ,
string >();
Student[ "Subject" ] = "php" ;
Display(Student);
Student[ "Subject" ] = "java" ;
Display(Student);
}
}
|
Output:
php
java
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...