tagged [equality]

What is an elegant way to check if 3 variables are equal when any of them can be a wildcard?

What is an elegant way to check if 3 variables are equal when any of them can be a wildcard? Say I have 3 `char` variables, `a`, `b` and `c`. Each one can be `'0'`, which is a special case and means i...

30 Apr at 12:35

Two different "strings" are the same object instance?

Two different "strings" are the same object instance? The code is pretty self explanatory. I expected when I made `a1` and `b1` that I was creating two different string instances that contain the same...

28 May at 04:57

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

Resharper suggestion: check for reference equality instead

Resharper suggestion: check for reference equality instead I don't understand why Resharper suggest me to "check for reference equality instead" in this code: Why this should be better: ------------ED...

30 Nov at 15:35

How do I compare strings in Java?

How do I compare strings in Java? I've been using the `==` operator in my program to compare all my strings so far. However, I ran into a bug, changed one of them into `.equals()` instead, and it fixe...

23 Jan at 13:36

(.1f+.2f==.3f) != (.1f+.2f).Equals(.3f) Why?

(.1f+.2f==.3f) != (.1f+.2f).Equals(.3f) Why? My question is about floating precision. It is about why `Equals()` is different from `==`. I understand why `.1f + .2f == .3f` is `false` (while `.1m + .2...

27 Feb at 19:45

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

== vs Equals in C#

== vs Equals in C# What is the difference between the evaluation of == and Equals in C#? For Ex, but Edited:

19 Mar at 14:47

Using a class versus struct as a dictionary key

Using a class versus struct as a dictionary key Suppose I had the following class and structure definition, and used them each as a key in a dictionary object: ``` public class MyClass { } public stru...

15 May at 03:40

Why is this code throwing an InvalidOperationException?

Why is this code throwing an InvalidOperationException? I think that my code should make the `ViewBag.test` property equal to `"No Match"`, but instead it throws an `InvalidOperationException`. Why is...

Equality between two enumerables

Equality between two enumerables I have two enumerables with the exact same reference elements, and wondering why Equals wouldn't be true. As a side question, the code below to compare each element wo...

20 May at 14:19

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

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 do these two comparisons have different results?

Why do these two comparisons have different results? Why does this code return true: but this code returns false:

15 Jan at 13:55

Why does ((object)(int)1).Equals(((object)(ushort)1)) yield false?

Why does ((object)(int)1).Equals(((object)(ushort)1)) yield false? I have the Situation that I have an `object` which I want to check for equality with another `object`. A Problem occurs when `a = 1 (...

14 Aug at 10:21

What's the difference between equal?, eql?, ===, and ==?

What's the difference between equal?, eql?, ===, and ==? I am trying to understand the difference between these four methods. I know by default that `==` calls the method `equal?` which returns true w...

8 Sep at 16:34

Why String.Equals is returning false?

Why String.Equals is returning false? I have the following C# code (from a library I'm using) that tries to find a certificate comparing the thumbprint. Notice that in the following code both `mycert....

23 Sep at 15:29

How do you test to see if a double is equal to NaN?

How do you test to see if a double is equal to NaN? I have a double in Java and I want to check if it is `NaN`. What is the best way to do this?

24 Dec at 08:14

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

What does this overload mean?

What does this overload mean? Can someone explain me what does this overload mean? I have never seen Object.ReferenceEquals in overloa

12 Jan at 07:32

Comparing arrays for equality in C++

Comparing arrays for equality in C++ Can someone please explain to me why the output from the following code is saying that arrays are ? ``` int main() { int iar1[] = {1,2,3,4,5}; int iar2[] = {1,...

19 Apr at 23:11

What is the purpose of casting into "object" type?

What is the purpose of casting into "object" type? I have found code on a website which is as follows. Here, what is the purpose of casting into object type again since a,b,d are itself the object

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

Two .NET objects that are equal don't say they are

Two .NET objects that are equal don't say they are I have the following code: What's up with that? Is the only way to fix this to go with .Equals() method?

13 Jul at 22:24

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