equals

find all occurrences of comparison with == in visual studio

余生长醉 提交于 2019-12-03 16:01:47
I made the mistake of using == for comparing IP addresses instead of using the equals() method of the IPAddress class in C#, which will result in the comparison of references instead of values. Since the solution I am currently working on is very large for a one-man project (> 100.000 lines of source code), I am very sure that I still have some of these wrong statements in my code. Is there any possibility to tell Visual Studio to find all occurrences of == operations on a specific class for me, so that I can find and clean up the bugged comparisons? with best regards, emi It's a bit of a hack

Example of ==, equals and hashcode in java

不问归期 提交于 2019-12-03 15:27:45
Given this: String s1= new String("abc"); String s2= new String("abc"); String s3 ="abc"; System.out.println(s1==s3); System.out.println(s1==s2); System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); System.out.println(s1.hashCode()); System.out.println(s2.hashCode()); System.out.println(s3.hashCode()); Output is: false false true true 96354 96354 96354 Here == is giving false for each object but the hashcode for each String object is same. Why is it so? == does compare real equality of objects (I mean - both references point to the same object), not their content, whereas

In javascript, what do multiple equal signs mean? [duplicate]

房东的猫 提交于 2019-12-03 15:22:09
问题 This question already has answers here : Javascript a=b=c statements (6 answers) Closed 5 years ago . I saw this code somewhere, but what does it mean? (all a, b, c are defined previously) var a = b = c; 回答1: It quickly assigns multiple variables to a single value. In your example, a and b are now equal set to the value of c . It's also often used for a mass assign of null to clean up. a = b = c = d = null; 回答2: Assign c to b. Assign b to a. So if I say var a = b = 1; >>> var a = b = 1;

Why does Java's Area#equals method not override Object#equals?

懵懂的女人 提交于 2019-12-03 14:53:39
问题 I just ran into a problem caused by Java's java.awt.geom.Area#equals(Area) method. The problem can be simplified to the following unit test: @org.junit.Test public void testEquals() { java.awt.geom.Area a = new java.awt.geom.Area(); java.awt.geom.Area b = new java.awt.geom.Area(); assertTrue(a.equals(b)); // -> true java.lang.Object o = b; assertTrue(a.equals(o)); // -> false } After some head scratching and debugging, I finally saw in the JDK source, that the signature of the equals method

Equals method implementation helpers (C#)

一笑奈何 提交于 2019-12-03 14:14:13
Everytime I write some data class, I usually spend so much time writing the IEquatable implementation. The last class I wrote was something like: public class Polygon { public Point[] Vertices { get; set; } } Implementing IEquatable was exaustive. Surely C#3.0/LINQ helps a lot, but the vertices can be shifted and/or in the reverse order, and that adds a lot of complexity to the Equals method. After many unit tests, and corresponding implementation, I gave up, and changed my application to accept only triangles, which IEquatable implementation required only 11 unit tests to be fully covered.

Does Dictionary.Equals() have an implementation?

最后都变了- 提交于 2019-12-03 12:19:46
I have a Dictionary which I am comparing to another Dictionary (variables typed as IDictionary). Doing d1.Equals(d2) yields false. Writing my own code below yields true. Both are System.Collections.Generic.Dictionary . Am I missing something or does Dictionary not have an Equals implementation that compares keys/values? private static bool DictEquals<K, V>(IDictionary<K, V> d1, IDictionary<K, V> d2) { if (d1.Count != d2.Count) return false; foreach (KeyValuePair<K, V> pair in d1) { if (!d2.ContainsKey(pair.Key)) return false; if (!Equals(d2[pair.Key], pair.Value)) return false; } return true;

Best practice to choose fields for equals() implementation

大城市里の小女人 提交于 2019-12-03 11:36:22
问题 When writing unit-tests, I often face the situation when equals() for some object in tests -- in assertEquals -- should work differently from how it works in actual environment. Take for example some interface ReportConfig . It has id and several other fields. Logically, one config equals to another one when their id s match. But when it comes to testing some specific implementation, say, XmlReportConfig , obviously I want to match all fields. One solution is not to use equals in tests and

Java: Clean way of avoiding NullPointerException in equals checks

痴心易碎 提交于 2019-12-03 10:47:29
I have an address object that I want to create an equals method for. I could have made this quite simple by doing something like the following (shortened a bit): public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Address other = (Address) obj; return this.getStreet().equals(other.getStreet()) && this.getStreetNumber().equals(other.getStreetNumber()) && this.getStreetLetter().equals(other.getStreetLetter()) && this.getTown().equals(other.getTown()); } Problem is, some of these might be null. I will in

“Not equal” sign in Visual Prolog?

自作多情 提交于 2019-12-03 10:32:00
I can't find any documentation on "not equal" sign in Visual Prolog. Please provide the right solution of this problem: class predicates sister : (string Person, string Sister) nondeterm(o,o). clauses sister(Person, Sister) :- Person [not-equal-sign] Sister, parent(Person, Parent), parent(Sister, Parent), woman(Sister). I don't know what do you mean by "not equal" (does not unify?), but you could try these: X \= Y not(X = Y) \+ (X = Y) Documentation for the second variant pointed out by Kaarel can be found in this Visual Prolog reference page. However the problem with your code goes a little

Java: Automatic equals() and hashCode()

妖精的绣舞 提交于 2019-12-03 10:01:57
Implementing equals() and hashCode() for simple data POJOs is cluttering my code and maintaining is tedious. What are the libraries handling this automatically? I prefer bytecode instrumentation over AOP approach due to performance reasons. Update: Topic of necessity of implementing equals() and hashCode() has been discussed, here's my point: Isn't it better to have it done right upfront with minimal effort rather than digging in the code, adding hC/eq when it comes to it? Project Lombok provides the annotation @EqualsAndHashCode which will generate equals() and hashCode() for your Java