equals-operator

Why does commuting the arguments of == in a console change the output?

回眸只為那壹抹淺笑 提交于 2019-12-22 05:02:49
问题 If I open my browser console (tested in Chrome/Firefox) and type: null == {} I get: false However, if I commute both arguments to the == operator and instead type: {} == null I get: Uncaught SyntaxError: Unexpected token == Image: Why does this happen? Why does this only happen in a console and not when the browser executes a script within an HTML page? EDIT: While question 35812626 addresses this and explains the cause as JS parsing the {} as a code block, it uses the triple equals (strict

C# what does the == operator do in detail?

☆樱花仙子☆ 提交于 2019-12-21 07:04:44
问题 in c# what does exactly happen in the background when you do a comparison with the "==" operator on two objects? does it just compare the addresses? or does it something like Equals() or CompareTo() ? PS: what about the "==" operator in java? does it behave the same? 回答1: As far as I know: it compares value types by value (equality) it compares reference types by reference (identity) except if the == operator is overloaded, then it calls that one. Equals is implemented in object and can be

Equality Test for Derived Classes in C++ [duplicate]

泄露秘密 提交于 2019-12-18 10:45:01
问题 This question already has answers here : Closed 10 years ago . Possible Duplicate: What’s the right way to overload operator== for a class hierarchy? In C++, how can derived classes override the base class equality test in a meaningful way? For example, say I have a base class A. Classes B and C derive from A. Now given two pointers to two A objects, can I test if they are equal (including any subclass data)? class A { public: int data; }; class B : public A { public: float more_data; bool

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

倾然丶 夕夏残阳落幕 提交于 2019-12-18 10:10:03
问题 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. 回答1: Other answers have already shown you that the behaviour is due to operator precedence, as documented here. I'm going to

What needs to be overridden in a struct to ensure equality operates properly?

大憨熊 提交于 2019-12-17 10:25:35
问题 As the title says: do I need to override the == operator? how about the .Equals() method? Anything I'm missing? 回答1: An example from msdn public struct Complex { double re, im; public override bool Equals(Object obj) { return obj is Complex && this == (Complex)obj; } public override int GetHashCode() { return re.GetHashCode() ^ im.GetHashCode(); } public static bool operator ==(Complex x, Complex y) { return x.re == y.re && x.im == y.im; } public static bool operator !=(Complex x, Complex y)

'==' vs string.equals c# .net [duplicate]

三世轮回 提交于 2019-12-10 14:13:15
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: C#: String.Equals vs. == Hi to all. Some time someone told me that you should never compare strings with == and that you should use string.equals(), but it refers to java. ¿What is the diference beteen == and string.equals in .NET c#? 回答1: string == string is entirely the same as String.Equals . This is the exact code (from Reflector): public static bool operator ==(string a, string b) { return Equals(a, b); //

Are all PHP equality comparisons symmetric?

ⅰ亾dé卋堺 提交于 2019-12-08 17:16:53
问题 Is $a == $b always equivalent to $b == $a ? I think in JavaScript there are a few weird cases where that's not true, due to casting. I think ide is correct. I'll ask another question. 回答1: Depends what happens between those two calls. Otherwise yes, those are the same. The order makes no difference. Using 2 equals == A string of 1 and integer of 1, will return true when compared. Type is ignored, only value is compared. So no wierdness. http://php.net/manual/en/language.operators.comparison

Comparing VALUE and REFERENCE of types

柔情痞子 提交于 2019-12-08 05:19:08
问题 I know there are a lot of ways to compare VALUE and REFERENCES in C#, but I'm still a bit confused about what type performs what when you try to compare either VALUE or REFERENCE. String examples: string str = "hello"; string str2 = "hello"; if (str == str2) { Console.WriteLine("Something"); } // Is this a comparison of value? if (str.Equals(str2)) { Console.WriteLine("Something"); } // Is this a comparison of value? string.ReferenceEquals(str, str2); // Comparison of reference (True) Console

What is best practice in Ruby to avoid misusing assignment “=”?

人盡茶涼 提交于 2019-12-07 18:14:38
问题 I've been bitten a couple of times by forgetting that x = y in Ruby makes x refer to the same object as y; I'm too used to languages where it means, in Ruby terms, x = y.dup . Forgetting this, I inadvertently change y when I think it's safe on the right side of the assignment. I can see that it would make sense to avoid simple x = y assignments without a special reason, but the same thing can be lurking in other places such as name = (person.last_name.blank? ? 'unknown' : person.last_name)

GetHashCode and Equals implementation in Dictionary C#

你。 提交于 2019-12-07 16:25:09
问题 I came to this site searching for object comparison in Dictionary, and i came to know that overriding GetHashCode and Equals are a must for doing object comparison in C#. Here is a piece of code that i have been trying to solve out, using FOREACH iteration Method. But my Boss says to do the same without using any iteration(maybe by using containskey or containsvalue method), due to performance issues. Any help is highly welcome.. public class employee { public string empname { get; set; }