CapyKit/Documentation/Help/T_CapyKit_PropertyComparer_2.md
2024-09-04 19:31:43 -05:00

4.6 KiB

PropertyComparer<T, U> Class

A object comparer that can accept a lambda expression to compare properties.

Definition

Namespace: CapyKit
Assembly: CapyKit (in CapyKit.dll) Version: 1.0.2

C#

public class PropertyComparer<T, U> : IEqualityComparer<T>

F#

type PropertyComparer<'T, 'U> = 
    class
        interface IEqualityComparer<'T>
    end
InheritanceObject → PropertyComparer(T, U)
ImplementsIEqualityComparer(T)

Type Parameters

Generic type parameter of the parent object.
Generic type parameter of the property value.

Example

using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { var people = new List<Person> { new Person { Name = "Alice", Age = 30 }, new Person { Name = "Bob", Age = 30 }, new Person { Name = "Charlie", Age = 35 } }; var comparer = new PropertyComparer<Person, int>(p => p.Age); var distinctPeople = people.Distinct(comparer).ToList(); foreach (var person in distinctPeople) { Console.WriteLine($"{person.Name} - {person.Age}"); } } } class Person { public string Name { get; set; } public int Age { get; set; } }

Constructors

PropertyComparer(T, U) Constructor.

Methods

Equals(Object) Determines whether the specified object is equal to the current object.
(Inherited from Object)
Equals(T, T) Determines whether the specified properties are equal.
Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object)
GetHashCode() Serves as the default hash function.
(Inherited from Object)
GetHashCode(T) Returns a hash code for the specified object.
GetType Gets the Type of the current instance.
(Inherited from Object)
MemberwiseClone Creates a shallow copy of the current Object.
(Inherited from Object)
ToString Returns a string that represents the current object.
(Inherited from Object)

Fields

expression The expression to retrieve the property.

See Also

Reference

CapyKit Namespace