Implementing EqualityCompare vs overriding GetHashCode and Equals

Implementing EqualityCompare vs overriding GetHashCode and Equals

Content Index :

Implementing EqualityCompare vs overriding GetHashCode and Equals
Tag : chash , By : Pitmairen
Date : November 23 2020, 03:01 PM

this will help I created two classes almost identical. Both represent a Pair (x,y) but in one of them I overrode the GetHashCode and Equals methods. I was told that when the HashCode is different the Collections takes them as different elements and does not even bother to actually compare them with the equals. However, it turns out that I implemented an EqualityComparer for the class that do not override the GetHashCode and Equals and everything works fine even when the HashCodes are still different. , Your problem is here
public override int GetHashCode(MyPairWithoutOverride i)
    return base.GetHashCode();

No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

NHibernate: Reasons for overriding Equals and GetHashCode

Tag : .net , By : anov
Date : March 29 2020, 07:55 AM
seems to work fine Overloading the Equals and GetHashCode methods is important if you are working with multiple sessions, detached entities, stateless sessions or collections (see Sixto Saez's answer for an example!).
In the same session scope identity map will ensure that you only have a single instance of the same entity. However, there is the possibility of comparing an entity with a proxy of the same entity (see below).

Simplify Overriding Equals(), GetHashCode() in C# for Better Maintainability

Tag : chash , By : Mare Astra
Date : March 29 2020, 07:55 AM
Any of those help MSDN actually doesn't say "don't overload Equals et al for mutable types". It used to say that, but now it says:

EF Hashset collection - Overriding Equals and GetHashCode

Tag : entity-framework , By : Ronnie Carlin
Date : March 29 2020, 07:55 AM
Does that help Why should I use Hashset?
You shouldn't. The uniqueness of a many-to-many row will be enforced at the database level so long as it's mapped properly. There is little to no benefit in enforcing it at the application level.
public class UserChartQuery
    public int UserId { get; set; }
    public int ChartQueryId { get; set; }

    public virtual User User { get; set; }
    public virtual ChartQuery ChartQuery { get; set; }

        protected bool Equals(UserChartQuery other)
        return UserId == other.UserId && ChartQueryId == other.ChartQueryId;

    public override bool Equals(object obj)
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != this.GetType()) return false;
        return Equals((UserChartQuery) obj);

    public override int GetHashCode()
            return (UserId*397) ^ ChartQueryId;
public class ChartQuery
    public int ChartQueryId { get; set; }
    public virtual ICollection<User> Users { get; set; }

public class User
    public int UserId { get; set; }
    public string UserName { get; set; }
    public virtual ICollection<ChartQuery> SavedChartQueries { get; set; }
    .HasMany(cq => ucq.Users)
    .WithMany(u => u.SavedChartQueries);

What is the best way to test for object equality - without overriding Equals & GetHashCode, or implementing IEquatab

Tag : chash , By : user109285
Date : March 29 2020, 07:55 AM

Overriding GetHashCode and Equals on a class because it's used in a dictionary

Tag : chash , By : foxthrot
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • How can I create Prototype Methods (like JavaScript) in C#.Net?
  • DataTable Loop Performance Comparison
  • CSV string handling
  • What is the best way to do unit testing for ASP.NET 2.0 web pages?
  • High availability
  • What to use for Messaging with C#
  • Accessing a Dictionary.Keys Key through a numeric index
  • ConfigurationManager.AppSettings Performance Concerns
  • What Are Some Good .NET Profilers?
  • Is this a good way to determine OS Architecture?
  • How to create a tree-view preferences dialog type of interface in C#?
  • Searching directories for tons of files?
  • Can I have a method returning IEnumerator<T> and use it in a foreach loop?
  • Why can't I have abstract static methods in C#?
  • Displaying ad content from Respose.WriteFile()/ Response.ContentType
  • Convert integers to written numbers
  • Absolute path back to web-relative path
  • How can we generate getters and setters in Visual Studio?
  • Bringing Window to the Front in C# using Win32 API
  • Possible to "spin off" several GUI threads? (Not halting the system at Application.Run)
  • Reading a C/C++ data structure in C# from a byte array
  • How should I translate from screen space coordinates to image space coordinates in a WinForms PictureBox?
  • Setting Objects to Null/Nothing after use in .NET
  • Converting ARBG to RGB with alpha blending
  • Is it better to create Model classes or stick with generic database utility class?
  • Passing enum type to Converter with integer value
  • Pool of objects with objects that are already on the scene in advance
  • StatusBar text fade-out when binding using Caliburn.Micro
  • Queryfilter on ApplicationUser in OnModelCreating in ApplicationDbContext creates StackOverflowException
  • How to get record form a different table based on a value from first table with linq expression?
  • Show data in Grid from returned model
  • Using Attributes to Override Data Model Conventions
  • Basic OOP console calculator, result Error
  • Compositon and Repository pattern
  • Multiple using statements with if condition
  • How do i increase a number by 1 in every line that contain the number 1
  • Add binding to elements that are created in codebehind
  • How to add a column in an existing AspNetUsers table
  • Order a list of elements with another list of doubles
  • How to setup a NuGet package to copy content files to output build directory?
  • In SignalR Core using ChannelWriter: Do I need to call TryComplete twice if there's an exception?
  • C# GetProcessesByName: issue with colon
  • c# wpf | create complex object with user-defined name to Serialize into JSON
  • How can I get a instance of a generic list with reflection?
  • WPF XAML - Design time and visibility of textbox
  • EF Core and MySql query is too slow
  • Getting Registered App Display Name from an App Id
  • How to get all variables from a string
  • Delete entity with all childs connected
  • Azure Build agent cant´t find class library referance
  • Initialize Nested Dictionaries in c#
  • .Net Core Binding
  • Generic event test method, preventing code duplication
  • How do I keep the ellipses in the center when the screen is resized
  • How to require a property using JsonSchema.NET?
  • C# XDocument Element/Elements returns null
  • Autofac keyed service with IEnumerable relationship type
  • Installing EntityFramework via NuGet manager
  • Always Check if there is Internet Connection Xamarin forms
  • WCF OneWay service slows down when aspNetCompatibilityEnabled is set to false
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com