tagged [equality]

Null checking is ambiguous for a class with several overrides for == operator

Null checking is ambiguous for a class with several overrides for == operator I have a class with two overrides for == operator, to compare it to other instances of this class, and to compare to insta...

Why comparing two strings as object causes unexpected result

Why comparing two strings as object causes unexpected result Consider the following piece of code. I understand the equality operator working here that as we have implicitly casted

13 Jul at 11:52

TypeDelegator equality inconsistency?

TypeDelegator equality inconsistency? Consider the following code: ``` class MyType : TypeDelegator { public MyType(Type parent) : base(parent) { } } class Program { static vo...

3 Oct at 09:34

Generic constraints: Can I test Equality of generic that can be a reference or value type?

Generic constraints: Can I test Equality of generic that can be a reference or value type? I want a single generic class that can accept either reference or value types, and only perform an action bas...

5 Oct at 12:42

Surprising Tuple (in)equality

Surprising Tuple (in)equality Until today, my understanding of .NET `Tuple` classes had been that they delegate their implementation of `Equals()` to their contents, allowing me to equate and compare ...

21 Feb at 18:24

Tuple vs string as a Dictionary key in C#

Tuple vs string as a Dictionary key in C# I have a cache that I implement using a ConcurrentDictionary, The data that I need to keep depends on 5 parameters. So the Method to get it from the cache is:...

Testing delegates for equality

Testing delegates for equality I'm building a hierarchical collection class that orders magnetic resonance images spatially and arranges them into groupings based on the various acquisition parameters...

4 Mar at 19:59

Is "(float)integer == integer" guaranteed to be equal in C#?

Is "(float)integer == integer" guaranteed to be equal in C#? While "we all know" that `x == y` can be problematic, where `x` and `y` are floating point values, this question is a bit more specific: No...

27 Sep at 20:20

GetHashCode() for OrdinalIgnoreCase-dependent string classes

GetHashCode() for OrdinalIgnoreCase-dependent string classes I'd like to implement a notion of distint addresses, so I overrode Equals() to test for case-insensitive equality in all of the fields (as ...

23 May at 10:29

How do I ignore the UTF-8 Byte Order Marker in String comparisons?

How do I ignore the UTF-8 Byte Order Marker in String comparisons? I'm having a problem comparing strings in a Unit Test in C# 4.0 using Visual Studio 2010. This same test case works properly in Visua...

Comparing boxed value types

Comparing boxed value types Today I stumbled upon an interesting bug I wrote. I have a set of properties which can be set through a general setter. These properties can be value types or reference typ...

23 May at 12:0

Comparing two structs using ==

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

4 Mar at 10:9

Correct way to override Equals() and GetHashCode()

Correct way to override Equals() and GetHashCode() I have never really done this before so i was hoping that someone could show me the correct what of implementing a override of Except() and GetHashCo...

28 Feb at 14:37

What is "Best Practice" For Comparing Two Instances of a Reference Type?

What is "Best Practice" For Comparing Two Instances of a Reference Type? I came across this recently, up until now I have been happily overriding the equality operator () and/or method in order to see...

What is the difference between using IEqualityComparer and Equals/GethashCode Override?

What is the difference between using IEqualityComparer and Equals/GethashCode Override? When i am using dictionaries sometimes I have to change the default Equals meaning in order to compare Keys. I s...

Should an implementation of IEqualityComparer.Equals allow null values?

Should an implementation of IEqualityComparer.Equals allow null values? I have a custom generic data structure that includes a Find method: I got a report recently from a client who said that i

19 Mar at 14:6

Why does Object.Equals() return false for identical anonymous types when they're instantiated from different assemblies?

Why does Object.Equals() return false for identical anonymous types when they're instantiated from different assemblies? I have some code that maps strongly-typed business objects into anonymous types...

31 Aug at 12:56

Implementing a geographic coordinate class: equality comparison

Implementing a geographic coordinate class: equality comparison I 'm integrating a geographic coordinate class from CodePlex to my personal "toolbox" library. This class uses `float` fields to store l...

21 Jul at 11:59

Compare PropertyInfo from Type.GetProperties() and lambda expressions

Compare PropertyInfo from Type.GetProperties() and lambda expressions While creating my testing framework I've found a strange problem. I want to create a static class that would allow me to compare o...

11 Apr at 20:38

Custom Equality check for C# 9 records

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. ...

14 Mar at 20:33