equals-operator

GetHashCode and Equals implementation in Dictionary C#

吃可爱长大的小学妹 提交于 2019-12-05 22:13:03
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; } public string location { get; set; } public double kinid { get; set; } public double managerKin { get; set

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

一个人想着一个人 提交于 2019-12-05 04:19:08
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 comparison) operator === , not the double equals == . As a user points out below, a code block can

Should I Overload == Operator?

ⅰ亾dé卋堺 提交于 2019-12-04 13:56:05
How does the == operator really function in C#? If it used to compare objects of class A , will it try to match all of A 's properties, or will it look for pointers to the same memory location (or maybe something else)? Let's create a hypothetical example. I'm writing an application that utilizes the Twitter API, and it has a Tweet class, which has all the properties of a single tweet: text, sender, date&time, source, etc. If I want to compare objects of class Tweet for equivalence, can I just use: Tweet a, b; if (a == b) { //do something... } Will that check for equivalence of all the

Boolean equal: 0 == a, does operand order matter?

独自空忆成欢 提交于 2019-12-04 04:04:50
问题 I saw some people wrote this Boolean equal in their code, I usually put constant at the right hand side of "==" operator. I notice 0 == a has faster operation than a == 0. Can someone explain why? And what's the best practice for it? 回答1: It's a relic of the C/C++ world. In C, the advantage of writing 0 == a vs. a == 0 is that you can't accidentally write a = 0 instead, which means something entirely different. Since 0 is an rvalue, 0 = a is illegal. In Java that reasoning does not apply

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

左心房为你撑大大i 提交于 2019-12-03 23:30:13
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? 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 overridden as well. The default implementation in Object performs a reference comparison for reference types.

Argument order for '==' with Nullable<T>

和自甴很熟 提交于 2019-12-03 11:09:23
问题 The following two C# functions differ only in swapping the left/right order of arguments to the equals operator, == . (The type of IsInitialized is bool ). Using C# 7.1 and .NET 4.7 . static void A(ISupportInitialize x) { if ((x as ISupportInitializeNotification)?.IsInitialized == true) throw null; } static void B(ISupportInitialize x) { if (true == (x as ISupportInitializeNotification)?.IsInitialized) throw null; } But the IL code for the second one seems much more complex. For example, B is

Argument order for '==' with Nullable<T>

别说谁变了你拦得住时间么 提交于 2019-12-03 01:37:16
The following two C# functions differ only in swapping the left/right order of arguments to the equals operator, == . (The type of IsInitialized is bool ). Using C# 7.1 and .NET 4.7 . static void A(ISupportInitialize x) { if ((x as ISupportInitializeNotification)?.IsInitialized == true) throw null; } static void B(ISupportInitialize x) { if (true == (x as ISupportInitializeNotification)?.IsInitialized) throw null; } But the IL code for the second one seems much more complex. For example, B is: 36 bytes longer (IL code); calls additional functions including newobj and initobj ; declares four

Why does an assignment in an if statement equate to true?

五迷三道 提交于 2019-12-02 16:02:59
问题 Let me start off by saying I understand the difference between = , == , and === . The first is used to assign the right-hand value to the left-hand variable, the second is used to compare the equivalency of the two values, and the third is used not just for equivalency but type comparison as well (ie true === 1 would return false ). So I know that almost any time you see if (... = ...) , there's a pretty good chance the author meant to use == . That said, I don't entirely understand what's

Why does an assignment in an if statement equate to true?

不问归期 提交于 2019-12-02 10:18:36
Let me start off by saying I understand the difference between = , == , and === . The first is used to assign the right-hand value to the left-hand variable, the second is used to compare the equivalency of the two values, and the third is used not just for equivalency but type comparison as well (ie true === 1 would return false ). So I know that almost any time you see if (... = ...) , there's a pretty good chance the author meant to use == . That said, I don't entirely understand what's happening with these scripts: var a = 5; if (a = 6) console.log("doop"); if (true == 2) console.log('doop

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

浪尽此生 提交于 2019-12-01 17:31:21
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> /// <param name="d">Data to be searched in all the nodes of a singly linked list /// Traverses through each node