Open In App

C# | Check if two ListDictionary objects are equal

Last Updated : 01 Feb, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Equals(Object) Method which is inherited from the Object class is used to check if a specified ListDictionary object is equal to another ListDictionary object or not.

Syntax:

public virtual bool Equals (object obj);

Here, obj is the object which is to be compared with the current object.

Return Value: This method return true if the specified object is equal to the current object otherwise it returns false.

Below programs illustrate the use of above-discussed method:

Example 1:




// C# code to check if two
// ListDictionary are equal or not
using System;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a ListDictionary named myDict
        ListDictionary myDict = new ListDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("Australia", "Canberra");
        myDict.Add("Belgium", "Brussels");
        myDict.Add("Netherlands", "Amsterdam");
        myDict.Add("China", "Beijing");
        myDict.Add("Russia", "Moscow");
        myDict.Add("India", "New Delhi");
  
        // Checking whether myDict is
        // equal to itself or not
        Console.WriteLine(myDict.Equals(myDict));
    }
}


Output:

True

Example 2:




// C# code to check if two
// ListDictionary are equal or not
using System;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a ListDictionary named myDict
        ListDictionary myDict1 = new ListDictionary();
  
        // Adding key/value pairs in myDict
        myDict1.Add("I", "first");
        myDict1.Add("II", "second");
        myDict1.Add("III", "third");
        myDict1.Add("IV", "fourth");
        myDict1.Add("V", "fifth");
  
        // Creating a ListDictionary named myDict2
        ListDictionary myDict2 = new ListDictionary();
  
        myDict2.Add("1st", "C");
        myDict2.Add("2nd", "C++");
        myDict2.Add("3rd", "Java");
        myDict2.Add("4th", "C#");
        myDict2.Add("5th", "HTML");
        myDict2.Add("6th", "PHP");
  
        // Checking whether myDict1 is
        // equal to myDict2 or not
        Console.WriteLine(myDict1.Equals(myDict2));
  
        // Creating a new ListDictionary
        ListDictionary myDict3 = new ListDictionary();
  
        // Assigning myDict2 to myDict3
        myDict3 = myDict2;
  
        // Checking whether myDict3 is
        // equal to myDict2 or not
        Console.WriteLine(myDict3.Equals(myDict2));
    }
}


Output:

False
True

Note: If the current instance is a reference type, the Equals(Object) method checks for reference equality.



Similar Reads

C# | Check if ListDictionary is synchronized (thread safe)
ListDictionary.IsSynchronized property is used to get a value indicating whether the ListDictionary is synchronized (thread safe) or not. Syntax: public bool IsSynchronized { get; } Return Value: This property always returns false. Below are the programs to illustrate the use of ListDictionary.IsSynchronized property: Example 1: // C# code to check
1 min read
C# | Check if ListDictionary is read-only
ListDictionary.IsReadOnly property is used to get a value indicating whether the ListDictionary is read-only or not. Syntax: public bool IsReadOnly { get; } Return Value : This property always returns false. Example: // C# code to check if ListDictionary is read-only using System; using System.Collections; using System.Collections.Specialized; clas
1 min read
C# | Check if ListDictionary has a fixed size
ListDictionary.IsFixedSize property is used to get a value indicating whether the ListDictionary has a fixed size or not. Syntax: public bool IsFixedSize { get; } Return Value : This property always returns false. Example: // C# code to check if ListDictionary // has a fixed size using System; using System.Collections; using System.Collections.Spec
1 min read
C# | Check if ListDictionary contains a specific key
ListDictionary.Contains(Object) method is used to check whether the ListDictionary contains a specific key or not. Syntax: public bool Contains (object key); Here, key is the key to locate in the ListDictionary. Return Value: The method returns true if the ListDictionary contains an entry with the specified key, otherwise it returns false. Exceptio
2 min read
C# | ListDictionary Class
.math-table { border-collapse: collapse; width: 100%; } .math-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .math-table th { border: 1px solid #5fb962; padding: 8px; } .math-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .math-table tr:nth-child(odd) { background-color: #ffffff; } ListDictiona
5 min read
C# | Remove all entries from the ListDictionary
ListDictionary.Clear method is used to remove the all entries from the ListDictionary. Syntax: public void Clear (); Below given are some examples to understand the implementation in a better way: Example 1: // C# code to remove all entries // from the ListDictionary using System; using System.Collections; using System.Collections.Specialized; clas
3 min read
C# | Remove the entry with specified key from ListDictionary
ListDictionary.Remove(Object) method is used to remove the entry with the specified key from the ListDictionary. Syntax: public void Remove (object key); Here, key is the key of the entry which is to be removed. Exception: This method will give ArgumentNullException if the key is null. Below are the examples to illustrate the use of ListDictionary.
3 min read
C# | Copy ListDictionary to Array instance at the specified index
ListDictionary.CopyTo(Array, Int32) method is used to copy the ListDictionary entries to a one-dimensional Array instance at the specified index. Syntax: public void CopyTo (Array array, int index); Parameters: array : It is the one-dimensional Array which is the destination of the DictionaryEntry objects copied from ListDictionary. The Array must
3 min read
C# | Add the specified key and value into the ListDictionary
ListDictionary.Add(Object, Object) method is used to add an entry with the specified key and value into the ListDictionary. Syntax: public void Add (object key, object value); Parameters: key : The key of the entry to add. value : The value of the entry to add. The value can be null. Exceptions: ArgumentNullException : If the key is null. ArgumentE
2 min read
C# | Get or set the value associated with specified key in ListDictionary
ListDictionary.Item[Object] property is used to get or set the value associated with the specified key. Syntax: public object this[object key] { get; set; } Here, key is the key whose value to get or set. Return Value : The value associated with the specified key. If the specified key is not found, attempting to get it returns null, and attempting
2 min read