Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1100303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:51:33+00:00 2026-05-17T00:51:33+00:00

i need to compare two objects but compare a number of their properties in

  • 0

i need to compare two objects but compare a number of their properties in one hit.
this is not for sorting, but instead to confirm whether anything has changed; as one is the old saved instance, and the second is a newly imported instance of the same thing

i assume this is best served by writing a custom comparer. just am a bit confused as to whether to do IComparer, or IComparable, or what tbh.

thanks

nat

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:51:34+00:00Added an answer on May 17, 2026 at 12:51 am

    If you only have a single definition of equality for your class, you don’t really need to implement any interface: simply override the Equalsmethod. Best practice though, would be to implement IEquatable<T>and to override GetHashCode sensibly (if you don’t override the hash-code, equality will misbehave when collection classes, LINQ methods etc. use it as a pre-condition for equality). Here’s a sample implementation:

    public class Person : IEquatable<Person>
    {
        public string Name { get; set; }
        public int Age { get; set; }
    
        public override int GetHashCode()
        {
            return (Name == null ? 0 : Name.GetHashCode()) ^ Age;
        }
    
        public override bool Equals(object obj)
        {
            return Equals(obj as Person);
        }
    
        public bool Equals(Person other)
        {
            return other != null && other.Name == Name && other.Age == Age;
        }
    }
    

    This will allow you to do:

    Person savedPerson = ...
    Person importedPerson = ...
    
    bool hasChanged = !savedPerson.Equals(importedPerson);
    

    If, on the other hand, you do have lots of different definitions of equality for different circumstances, your best bet would be to write up different IEqualityComparer<T>implementations. Here’s a sample implementation:

    public class AgeComparer : IEqualityComparer<Person>
    {
        public bool Equals(Person x, Person y)
        {
            return (x == null || y == null) ? x == y : x.Age == y.Age;
        }
    
        public int GetHashCode(Person obj)
        {
            return obj == null ? 0 : obj.Age;
        }
    }  
    

    In this case, the check will look like:

    Person savedPerson = ...
    Person importedPerson = ...
    IEqualityComparer<Person> comparer = ...
    
    bool hasChanged = !comparer.Equals(savedPerson, importedPerson);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to sort two objects in a list by using two properties one by
I need to compare the integer part of two doubles for inequality and I'm
I need to compare two dates using the Oracle decode function to see if
I need to compare two numbers and look for similarities in more significant bits.
In my Java application i need to compare two list's element whether it is
Suppose I've got a generic MyClass<T> that needs to compare two objects of type
i've two objects of different that need to be compared for equality I can
I need to compare strings in shell: var1=mtu eth0 if [ $var1 == mtu
I need to compare 2 strings as equal such as these: Lubeck == Lübeck
I need to compare the answer in with the aspnet_membership tables PasswordAnswer value. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.