What is the "best" canonical implementation of Equals() for reference types?

Implementing Equals() for reference types is harder than it seems. My current canonical implementation goes like this: public bool Equals( MyClass obj ) { // If both refer to the same reference...

Why is '397' used for ReSharper GetHashCode override?

Like many of you, I use ReSharper to speed up the development process. When you use it to override the equality members of a class, the code-gen it produces for GetHashCode() looks like: ...

What is the best algorithm for overriding GetHashCode?

In .NET, the GetHashCode method is used in a lot of places throughout the .NET base class libraries. Implementing it properly is especially important to find items quickly in a collection or when...

Why is it important to override GetHashCode when Equals method is overridden?

Given the following class public class Foo { public int FooId { get; set; } public string FooName { get; set; } public override bool Equals(object obj) { Foo fooItem = obj...

Is there any way in C# to override a class method with an extension method?

There have been occasions where I would want to override a method in a class with an extension method. Is there any way to do that in C#? For example: public static class StringExtension { ...

Member '<member name>' cannot be accessed with an instance reference

I am getting into C# and I am having this issue: namespace MyDataLayer { namespace Section1 { public class MyClass { public class MyItem { ...

IEnumerable.Except() and a custom comparer

I'm having troubles with the Except() method. Instead of returning the difference, it returns the original set. I've tried implementing the IEquatable and IEqualityComparer in the Account...

Quick question about a reference type key in a generic dictionary in .Net

I have a mutable class that I'm using as a key to a generic dictionary. Two keys should be equal only if their references are equal. From what I've read, in this case, I don't need to override...

C# - Generic HashCode implementation for classes

I'm looking at how build the best HashCode for a class and I see some algorithms. I saw this one : Hash Code implementation, seems to be that .NET classes HashCode methods are similar (see by...

How to generate a unique hash code for an object, based on its contents?

I need to generate a unique hash code for an object, based on its contents, e.g. DateTime(2011,06,04) should equal DateTime(2011,06,04). I cannot use .GetHashCode() because it might generate the...

DataGrid identity problem when bound to list with overridden IsEqual method

Picture the following scenario: I have a DataGrid which I've bound to a list containing objects. Each object represents a row with various data (name, customer, server name etc.) Now let's say...

What is hashCode used for? Is it unique?

I notice there is a getHashCode() method in every controls, items, in WP7, which return a sequence of number. Can I use this hashcode to identify an item? For example I want to identify a picture...

IEqualityComparer<double> with a tolerance; how to implement GetHashCode?

I'm implementing a reusable DoubleEqualityComparer (with a custom tolerance: the "epsilon" constructor parameter) to ease the usage of LINQ with sequences of double. For example: bool...

Equating derived classes based on their base's Equals()

I have two classes which both derive from the same parent: public class People{ public string BetterFoot; public override bool Equals(object obj){ if (obj == null ||...

Comparing two structs using ==

I am trying to compare two structs using equals (==) in C#. My struct is below: public struct CisSettings : IEquatable<CisSettings> { public int Gain { get; private set; } public int...

Non-readonly fields referenced in GetHashCode()

Started with overriding concepts and I override the methods Equals and GetHashCode. Primarily I came up with this "very simple code": internal class Person { public string name; ...

DDD: Entity identity before being persisted

In Domain Driven Design, one of the defining characteristic of an Entity is that it has an identity. Problem: I am not able to provide a unique identity to Entities on instance creation. This...

C# Compare two dictionaries for equality

I want to compare in C# two dictionaries with as keys a string and as value a list of ints. I assume two dictionaries to be equal when they both have the same keys and for each key as value a list...

Tuple's GetHashCode hack

Knowing that to obtain hashcode for two objects it is common practice to do XOR of their respective hashcodes I wanted to check how Tuple deals with situation where Item1 == Item2. This is what I...

How do I make a struct immutable?

All over Stack Overflow and the internet I see that it is a good design principle to keep structs immutable. Unfortunately, I never see any implementation that actually causes these structs to be...

Is it possible to override the Getter/Setter functions in a powershell 5 class?

I recently started creating classes with powershell 5. While I was following this awesome guide https://xainey.github.io/2016/powershell-classes-and-concepts/#methods I was wondering if it is...

IndexOf with custom StringComparer

Why does String.IndexOf(String, StringComparison) require a StringComparison and not allow for the more general StringComparer, or even just IComparer<T> or IEqualityComparer<T>? I made a custom...

parsedhtml doesnt respond anymore

So I'm trying to get some text from a website, and as soon as I try to return an object with ParsedHtml, powershell stops responding (even when I let it run in the background a few minutes it wont...

IEquatable<Point3D> within a tolerance, how to implement GetHashCode()

I have a Point3d struct which implements IEquatable<Point3d> in the following manner: public override bool Equals(object obj) { return obj is Point3d p && Equals(p); } public bool...

Caliburn.micro get treeview's selected item by contextmenu onclick

I have a task to use a contextmenu in treeview and pass selected treeview's item to ViewModel by clicking on contextmenu element. Here is my xaml: <Window.Resources> <HierarchicalDataTemplate...

What is equivalent for Java Objects.hash and Objects.hashCode in C#

Moving from Java to C# developer. In Java, I used a lot of Objects.hash(array) and Objects.hashCode(object) to build hash code of an object in hashCode function. I can't find any equivalent for...

What is the best practice to implement equality for readonly structs?

I've just started to program in C# last year and I'm still learning the language. I have a question regarding readonly struct type and equality comparison methods. When creating a struct in C#, I...

Custom Equality check for C# 9 records

From what I understand, records are actually classes that implement their own equality check in a way that your object is value-driven and not reference driven. In short, for the record Foo that...

Cannot use new HashCode to override GetHashCode in class

I have copied the code that the professor gave me, and I got the error "The name 'HashCode does not exist in the current context". I read about it something and I think it should work. I am using...

Flutter: TableCalendar how to display events from Firebase FireStore

I'm making app with Calendar. As calendar i'm using Table Calendar. I also use FireStore in my app. After implementing TableCalendar i followed github demo to display events Now i want to use...