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 4612664
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:25:59+00:00 2026-05-22T01:25:59+00:00

I have the following code using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace

  • 0

I have the following code

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            List<I> bars = new List<I>();
            bars.Add(new A() { Id = 1 });
            bars.Add(new B() { Id = 1 });
            bars.Add(new A() { Id = 1 });

            var distictBars = bars.Distinct();

            foreach (var item in distictBars)
            {
                Debug.WriteLine(item.Name);
            }
        }
    }

    interface I
    {
        string Name { get; }
    }

    class Base
    {
        public int Id { get; set; }
    }

    class A : Base, I, IEquatable<A>
    {
        public string Name
        {
            get { return this.Id + "-A"; }
        }

        public bool Equals(A other)
        {
            return this.Id == other.Id;
        }

        public override bool Equals(object obj)
        {
            if (obj is A)
                return this.Equals(obj as A);
            else
                return object.ReferenceEquals(this, obj);
        }
    }

    class B : Base, I, IEquatable<B>
    {
        public string Name
        {
            get { return this.Id + "-B"; }
        }

        public bool Equals(B other)
        {
            return this.Id == other.Id;
        }

        public override bool Equals(object obj)
        {
            if (obj is A)
                return this.Equals(obj as A);
            else
                return object.ReferenceEquals(this, obj);
        }
    }
}

The output is the following

1-A 
1-B 
1-A 

I need however the following

1-A 
1-B

that is, eliminate the duplicates of type A or B. It seems that the Distinct LINQ method didn’t take in consideration my Equals override, nor the IEquitable interface implementation.

How should I proceed in order than that method does the specified comparison?

  • 1 1 Answer
  • 1 View
  • 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-22T01:25:59+00:00Added an answer on May 22, 2026 at 1:25 am

    The Distinct method will likely use something similar to a HashSet<T> internally, which relies on a hash code (obviously). You will need to also override GetHashCode.


    It seems to me you have some unnecessary duplication in general. Both your A and B classes share basically the same logic for determining equality; is what you want to ensure that two Base objects are equal if their IDs are equal and they are the same type? (This is what it looks like to me; your implementation seems to contain a couple of mistakes in this case, but that’s still my best guess.)

    If so, just make Base implement IEquatable<Base> and your A and B implementations become much simpler:

    class Base : IEquatable<Base>
    {
        public Base(int id)
        {
            Id = id;
        }
    
        public int Id { get; private set; }
    
        public bool Equals(Base other)
        {
            if (other == null)
            {
                return false;
            }
    
            // Ensure As only compare equal with other As
            // and the same for Bs (and potentially other subtypes).
            return other.GetType().Equals(GetType()) && other.Id == Id;
        }
    
        public override bool Equals(object other)
        {
            if (other is Base)
            {
                return Equals((Base)other);
            }
    
            return false;
        }
    
        public override int GetHashCode()
        {
            return Id;
        }
    }
    
    class A : Base, I
    {
        public A(int id) : base(id)
        { }
    
        public string Name
        {
            get { return this.Id + "-A"; }
        }
    }
    
    class B : Base, I
    {
        public B(int id) : base(id)
        { }
    
        public string Name
        {
            get { return this.Id + "-B"; }
        }
    }
    

    Notice that I also changed the setter on the Id property to private; in general, it’s a very risky game to make a field or property publicly writable when it’s used in the type’s GetHashCode implementation (since if an object’s hash code ever changes, it stops working as a key in a hashtable).


    This comment becomes unnecessary if you apply my recommendation above.

    Also, warning! It looks to me like you’ve got a risk of infinite recursion in your B.Equals method:

        public override bool Equals(object obj)
        {
            if (obj is A)
                // There is no method with the signature Equals(A);
                // so this will just call your Equals(object) method --
                // i.e., THIS method -- over and over again.
                return this.Equals(obj as A);
            else
                return object.ReferenceEquals(this, obj);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code: using System.Collections.Generic; static class Program { static void Main() {
I have the following code : using System.Collections.Generic; public class Test { static void
I have the following code: using System; using System.Linq; using System.Linq.Expressions; public class Program
I have the following code: using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; public
I have the following code in my HomeController.cs using System; using System.Collections.Generic; using System.Linq;
I have the following code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using
Suppose I have the following class: using System; using System.Collections.Generic; using System.Linq; using System.Text;
I have the following code: using System; using System.Diagnostics; using System.IO; using PdfSharp.Pdf.Printing; namespace
i have the following code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using
i have the following code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using

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.