equals-operator

Why does == not work while comparing two object type variables boxed with same int value

感情迁移 提交于 2019-12-01 17:05:10
问题 While trying to implement a simple singly linked list in C#, I noticed that == does not work while comparing two object type variables boxed with an int value but .Equals works. Wanted to check why that is so. The below snippet is a generic object type Data property public class Node { /// <summary> /// Data contained in the node /// </summary> private object Data { get; set; }; } The below code traverses the singly linked list and searches for a value of type object - /// <summary> ///

Equality operator overloads: Is (x!=y) == (!(x==y))?

安稳与你 提交于 2019-12-01 13:42:52
问题 Does the C++ standard guarantee that (x!=y) always has the same truth value as !(x==y) ? I know there are many subtleties involved here: The operators == and != may be overloaded. They may be overloaded to have different return types (which only have to be implicitly convertible to bool ). Even the ! -operator might be overloaded on the return type. That's why I handwavingly referred to the "truth value" above, but trying to elaborate it further, exploiting the implicit conversion to bool ,

Is it necessary to override == and != operators when overriding the Equals method? (.NET)

和自甴很熟 提交于 2019-11-30 23:50:30
问题 Or it's advisable to do that? Why? 回答1: See the guidelines for overriding Equals() and operator==. Quote: By default, the operator == tests for reference equality by determining whether two references indicate the same object. Therefore, reference types do not have to implement operator == in order to gain this functionality. When a type is immutable, that is, the data that is contained in the instance cannot be changed, overloading operator == to compare value equality instead of reference

C# implicit conversions and == operator

一世执手 提交于 2019-11-30 15:15:30
问题 Some code for context: class a { } class b { public a a{get;set;} public static implicit operator a(b b) { return b.a; } } a a=null; b b=null; a = b; //compiler: cannot apply operator '==' to operands of type tralala... bool c = a == b; Is it possible to use == operator on different type instances, where one can implicitly convert to another? What did i miss? Edit: If types must be the same calling ==, then why int a=1; double b=1; bool c=a==b; works? 回答1: The implicit operator only works for

Difference between == and === in Mathematica

房东的猫 提交于 2019-11-30 12:45:22
I was under the impression that = is an assignment, == is a numeric comparison, and === is a symbolic comparison (as well as in some other languages == being equal to and === being identical to . However, looking at the following it would appear that this is not necessarily the case... In: x == x Out: True In: x === x Out: True In: 5 == 5 Out: True In: 5 === 5 Out: True In: x = 5 Out: 5 In: 5 == x Out: True In: 5 === x Out: True In: 5 5 == 5x Out: True In: 5 5 === 5x Out: True In: x == y Out: x == y In: x === y Out: False In: y = x Out: 5 In: x == y Out: True In: x === y Out: True So what

Is there a reason not to use <=> (null safe equals operator) in mysql instead of =?

送分小仙女□ 提交于 2019-11-30 12:24:22
MySQL provides a nice operator <=> that works with comparisons that could contain a null such as null <=> null or null <=> 5 etc. giving back intuitive results as many programming languages. Whereas the normal equals operator always just returns null, which catches many new MySQL users such as myself awry. Is there a reason MySQL has both and not JUST the functionality in <=> ? Who really needs an operator that is effectively undefined with built in language types? Who really needs an operator that is effectively undefined with built in language types? You asked for some real-world examples.

Performance differences between equal (=) and IN with one value

断了今生、忘了曾经 提交于 2019-11-30 12:24:00
问题 How does SQL engines differ when we use equal sign and IN operator have same value? Does execution time changes? 1st one using equality check operator WHERE column_value = 'All' 2nd one using OR operator and single value WHERE column_value IN ('All') Does SQL engine changes IN to = if only one value is there? Is there any difference for same in MySQL and PostgreSQL? 回答1: There is no difference between those two statements, and the optimiser will transform the IN to the = when IN have just one

Does == check for full equality in Booleans? - Java

左心房为你撑大大i 提交于 2019-11-30 11:09:06
So I've heard that if I compare 2 strings with == then I will only get true back if they both refer to the same object/instance. That's strings. What about Booleans? Does == check for full equality in Booleans? - Java It depends on whether you're talking about Boolean s (the object wrapper, note the capital B ) or boolean s (the primitive, note the lower case b ). If you're talking about Boolean s (the object wrapper), as with all objects, == checks for identity , not equivalence . If you're talking about boolean s (primitives), it checks for equivalence. So: Boolean a, b; a = new Boolean

Performance differences between equal (=) and IN with one value

为君一笑 提交于 2019-11-30 02:37:51
How does SQL engines differ when we use equal sign and IN operator have same value? Does execution time changes? 1st one using equality check operator WHERE column_value = 'All' 2nd one using OR operator and single value WHERE column_value IN ('All') Does SQL engine changes IN to = if only one value is there? Is there any difference for same in MySQL and PostgreSQL? sagi There is no difference between those two statements, and the optimiser will transform the IN to the = when IN have just one element in it. Though when you have a question like this, just run both statements, run their

What's the meaning of “(1,) == 1,” in Python?

牧云@^-^@ 提交于 2019-11-29 20:24:35
I'm testing the tuple structure, and I found it's strange when I use the == operator like: >>> (1,) == 1, Out: (False,) When I assign these two expressions to a variable, the result is true: >>> a = (1,) >>> b = 1, >>> a==b Out: True This questions is different from Python tuple trailing comma syntax rule in my view. I ask the group of expressions between == operator. wim Other answers have already shown you that the behaviour is due to operator precedence, as documented here . I'm going to show you how to find the answer yourself next time you have a question similar to this. You can