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#
// 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
Similar Reads
C# Program to Demonstrate the IList Interface In C# IList interface is an interface that belongs to the collection module where we can access each element by index. Or we can say that it is a collection of objects that are used to access each element individually with the help of an index. It is of both generic and non-generic types. Its implem
3 min read
C# Program to Demonstrate the Inheritance of Abstract Classes Abstraction is the process to hide the internal details and show only the functionality. The abstract keyword is used before the class or method to declare the class or method as abstract. And inheritance is the object-oriented programming methodology by which one class is allowed to inherit the fea
2 min read
C# Program to Implement Multiple-Inheritance using Abstract Class and Interface Given multiple inheritance, now our task is to implement multiple inheritance with the help of abstract class and interface. So, before implementation first of all we learn the basic definition of multiple-inheritance, abstract class, and interface. Multiple inheritance: Multiple inheritance is a ty
4 min read
C# Program to Check a Specified Type is an Interface or not The interface is just like a class, it can also have methods, properties, events, etc. as its members, but it only contains the declaration of the members and the implementation of these members will be given by the class that implements the interface implicitly or explicitly. We can check the speci
2 min read
C++ Program to Create an Interface Interfaces are a feature of Java that allows us to define an abstract type that defines the behaviour of a class. In C++, there is no concept of interfaces, but we can create an interface-like structure using pure abstract classes. In this article, we will learn how to create interface-like structur
2 min read
C# Program to Demonstrate Interface Implementation with Multi-level Inheritance Multilevel Inheritance is the process of extending parent classes to child classes in a level. In this type of inheritance, a child class will inherit a parent class, and as well as the child class also act as the parent class to other class which is created. For example, three classes called P, Q,
3 min read