C# | Check if two LinkedList<T> objects are equal
Last Updated :
01 Feb, 2019
Equals(Object) Method which is inherited from the Object class is used to check if a specified LinkedList<T> object is equal to another LinkedList<T> 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:
using System;
using System.Collections.Generic;
class Geeks {
public static void Main(String[] args)
{
LinkedList<String> myList1 = new LinkedList<String>();
myList1.AddLast( "Geeks" );
myList1.AddLast( "for" );
myList1.AddLast( "Data Structures" );
myList1.AddLast( "Noida" );
Console.WriteLine(myList1.Equals(myList1));
}
}
|
Example 2:
using System;
using System.Collections.Generic;
class Geeks {
public static void Main(String[] args)
{
LinkedList<String> myList1 = new LinkedList<String>();
myList1.AddLast( "C" );
myList1.AddLast( "C++" );
myList1.AddLast( "Java" );
myList1.AddLast( "C#" );
LinkedList< int > myList2 = new LinkedList< int >();
myList2.AddLast(2);
myList2.AddLast(4);
myList2.AddLast(6);
myList2.AddLast(8);
Console.WriteLine(myList1.Equals(myList2));
LinkedList< int > myList3 = new LinkedList< int >();
myList3 = myList2;
Console.WriteLine(myList3.Equals(myList2));
}
}
|
Note: If the current instance is a reference type, the Equals(Object) method checks for reference equality.
Share your thoughts in the comments
Please Login to comment...