LINQ in C# - Non-Deferred Operator Examples- SequenceEqual - Equality

SequenceEqual

  • The SequenceEqual operator determines whether two input sequences are equal.

Type 1 - Example 1

 List<int> ItemSeq1 = new List<int> { 1, 2, 3 };
 List<int> ItemSeq2 = new List<int> { 1, 2, 3 };

            Console.WriteLine(ItemSeq1.Equals(ItemSeq2));
            Console.WriteLine(ItemSeq1.Equals(ItemSeq1));
//Output
false
true
  • How SequenceEqual it works?
  • SequenceEqual returns true 
    • If the two source sequences are of equal length and their corresponding elements are equal according to the default equality comparer for their type

Type 1 - Example 2

class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}

public static void SequenceEqualEx1()
{
Pet pet1 = new Pet { Name = "Turbo", Age = 2 };
Pet pet2 = new Pet { Name = "Peanut", Age = 8 };
Pet pet3 = new Pet { Name = "Turbo", Age = 2 };
Pet pet4 = new Pet { Name = "Peanut", Age = 8 };

// Create two lists of pets.
List<Pet> pets1 = new List<Pet> { pet1, pet2 };
List<Pet> pets2 = new List<Pet> { pet1, pet2 };
List<Pet> pets3 = new List<Pet> { pet3, pet4 };
List<Pet> pets4 = new List<Pet> { pet3, pet4 };

bool Areequal1 = pets1.SequenceEqual(pets2);
bool Areequal2 = pets3.SequenceEqual(pets4);
bool Areequal3 = pets1.SequenceEqual(pets3);
bool Areequal4 = pets1.SequenceEqual(pets3);

Console.WriteLine("The lists {0} equal.", Areequal1 ? "are" : "are not");
Console.WriteLine("The lists {0} equal.", Areequal2 ? "are" : "are not");
Console.WriteLine("The lists {0} equal.", Areequal3 ? "are" : "are not");
Console.WriteLine("The lists {0} equal.", Areequal4 ? "are" : "are not");

}
 
//Output
The lists are equal.
The lists are equal.
The lists are not equal.
The lists are not equal.

Type 2 - Example 1 using IEquatable Interface

  • If you want to compare the actual data of the objects in the sequences instead of just comparing their references, you have to implement the IEquatable Interface generic interface in your class.
class Pet : IEquatable<Pet>
{
public string Name { get; set; }
public int Age { get; set; }

public bool Equals(Pet other)
{
if (other is null)
return false;

return this.Name == other.Name && this.Age == other.Age; // Compare with Values
}
}


public static void SequenceEqualEx1()
{
Pet pet1 = new Pet { Name = "Turbo", Age = 2 };
Pet pet2 = new Pet { Name = "Peanut", Age = 8 };
Pet pet3 = new Pet { Name = "Turbo", Age = 2 };
Pet pet4 = new Pet { Name = "Peanut", Age = 8 };

// Create two lists of pets.
List<Pet> pets1 = new List<Pet> { pet1, pet2 };
List<Pet> pets2 = new List<Pet> { pet1, pet2 };
List<Pet> pets3 = new List<Pet> { pet3, pet4 };
List<Pet> pets4 = new List<Pet> { pet3, pet4 };

bool Areequal1 = pets1.SequenceEqual(pets2);
bool Areequal2 = pets3.SequenceEqual(pets4);
bool Areequal3 = pets1.SequenceEqual(pets3);
bool Areequal4 = pets1.SequenceEqual(pets3);

Console.WriteLine("The lists {0} equal.", Areequal1 ? "are" : "are not");
Console.WriteLine("The lists {0} equal.", Areequal2 ? "are" : "are not");
Console.WriteLine("The lists {0} equal.", Areequal3 ? "are" : "are not");
Console.WriteLine("The lists {0} equal.", Areequal4 ? "are" : "are not");

}
 
//Output
The lists are equal.
The lists are equal.
The lists are equal.
The lists are equal.

Type 2 - Example 2 using IEqualityComparer Interface

class PetComparer : IEqualityComparer<Pet>
    {
        public bool Equals(Pet x, Pet y)
        {
            if (x is null || y is null)
                return false;

            return x.Name == y.Name && x.Age == y.Age; // Compare with Values
        }

        public int GetHashCode(Pet obj)
        {
            return obj.Name.Length; // Just for returning some integer, there is no logic used here
        }
    }
    class Pet
    {
        public string Name { get; set; }
        public int Age { get; set; }       
    }


            Pet pet1 = new Pet { Name = "Turbo", Age = 2 };
            Pet pet2 = new Pet { Name = "Peanut", Age = 8 };
            Pet pet3 = new Pet { Name = "Turbo", Age = 2 };
            Pet pet4 = new Pet { Name = "Peanut", Age = 8 };

            // Create two lists of pets.
            List<Pet> pets1 = new List<Pet> { pet1, pet2 };
            List<Pet> pets2 = new List<Pet> { pet1, pet2 };
            List<Pet> pets3 = new List<Pet> { pet3, pet4 };
            List<Pet> pets4 = new List<Pet> { pet3, pet4 };

            bool Areequal1 = pets1.SequenceEqual(pets2, new PetComparer());
            bool Areequal2 = pets3.SequenceEqual(pets4, new PetComparer());
            bool Areequal3 = pets1.SequenceEqual(pets3, new PetComparer());
            bool Areequal4 = pets1.SequenceEqual(pets3, new PetComparer());

            Console.WriteLine("The lists {0} equal.", Areequal1 ? "are" : "are not");
            Console.WriteLine("The lists {0} equal.", Areequal2 ? "are" : "are not");
            Console.WriteLine("The lists {0} equal.", Areequal3 ? "are" : "are not");
            Console.WriteLine("The lists {0} equal.", Areequal4 ? "are" : "are not");
//Output
The lists are equal.
The lists are equal.
The lists are equal.
The lists are equal.

No comments:

Post a Comment

Framework Fundamentals - String - Comparing Strings

In comparing two values, the .NET Framework differentiates the concepts of equality comparison and order comparison . Equality compariso...