equality

Equals int and short c# returns false [duplicate]

岁酱吖の 提交于 2019-12-10 18:01:28
问题 This question already has answers here : What is the difference between == and Equals() for primitives in C#? (9 answers) Closed 4 years ago . Consider this code: static int x2 = 10; public static void Main() { short y = 10; Console.WriteLine(y.Equals(x2)); //False Console.Read(); } Why y.Equals(x2) returns false ? 回答1: Int16.Equals specific docs Return Value true if obj is an instance of Int16 and equals the value of this instance; otherwise, false. This was my original answer, whilst it

Doesnt stl sort require a strict weak ordering to work?

﹥>﹥吖頭↗ 提交于 2019-12-10 17:56:05
问题 From http://stdcxx.apache.org/doc/stdlibref/less-equal.html -- You can pass a less_equal object to any algorithm that requires a binary function. For example, the sort() algorithm can accept a binary function as an alternate comparison object to sort a sequence. less_equal would be used in that algorithm in the following manner: vector<int> vec1; sort(vec1.begin(), vec1.end(),less_equal<int>()); -- Now I am confused, is the documentation above correct ? 回答1: You are right, std::sort requires

Entity Framework: Is there a method to compare for equal values of EntityObject?

╄→гoц情女王★ 提交于 2019-12-10 16:41:52
问题 Is there a simple way to compare two EntityObjects for value-equality. I simply want to check if all the database-values are the same, so I don't care if the EntityKey is different. Is this possible built-in? Or should I just write my own method. I guess Equals() doesn't work as I want it here? 回答1: Equals() checks for referential equality, so it wouldn't help you there. Take a look at this question: What is the best way to compare two entity framework entities? UPDATE 2014: A more complete

Minimal code for equality in C#

余生颓废 提交于 2019-12-10 15:55:04
问题 In this article, Eric Lippert suggests in point #9 that C# has "too much equality". He points out that there are 9 or 10 different methods or operators that can be overloaded to provide object equality. My first question is - if the Object.Equals(object) method is overridden, is it possible for the compiler to call any of the other equality operators like ==, !=, <=, etc. without code that expressly performs this operation? In C++, there is precedent for this behavior. The copy constructor

Does null == null?

泪湿孤枕 提交于 2019-12-10 14:40:06
问题 I have an object of type Foo . Foo has an Id (int) a) Is the code bellow "good"? b) What should I return if both are null? // overload operator == public static bool operator ==(Foo a, Foo b) { if (ReferenceEquals(x, y)) { return true; } if (x == null && y == null) { return // ??? } if (x == null || y == null) { return false; } return x.Id == y.Id; // Ids are the same } public static bool Equals(Foo x, Foo y) { return x == y; } EDIT: c) Should the Equals method call the == operator , or

How can I deep-compare 2 Lua tables, which may or may not have tables as keys?

删除回忆录丶 提交于 2019-12-10 14:15:28
问题 (Also posted on the Lua mailing list) So I've been writing deep-copy algorithms, and I wanna test them to see if they work the way I want them to. While I do have access to the original->copy map, I want a general-purpose deep-compare algorithm that must be able to compare table keys (tables as keys?). My deep-copy algorithm(s) are avaliable here: https://gist.github.com/SoniEx2/fc5d3614614e4e3fe131 (it's not very organized, but there are 3 of them, one uses recursive calls, the other uses a

How to Write an Equality Method in Java

£可爱£侵袭症+ 提交于 2019-12-10 13:58:31
问题 Consider adding an equality method to the following class of simple points: public class Point { private final int x; private final int y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } // ... } // my definition of equals public boolean equals(Point other) { return (this.getX() == other.getX() && this.getY() == other.getY()); } What's wrong with this method? At first glance, it seems to work OK: Point p1 = new Point(1, 2)

CollectionAssert.AreEquivalent vs Assert.Equals()

点点圈 提交于 2019-12-10 12:50:01
问题 public void NumericListCanBeSorted() { var sorted = sort.SortNumbers(nums); Assert.AreEqual(sorted, nums); } public List<int> SortNumbers(List<int> nums) { List<int> sortedList = new List<int>(); for (int i = 0; i < nums.Count(); i++) { for (int j = i + 1; j < nums.Count; j++) { if (nums[i] > nums[j]) { //Swapping values nums[i] = nums[i] + nums[j]; nums[j] = nums[i] - nums[j]; nums[i] = nums[i] - nums[j]; } } sortedList.Add(nums[i]); } return sortedList; } Result Message: Assert.AreEqual

Java: clone() and equality checks

匆匆过客 提交于 2019-12-10 09:59:10
问题 Perhaps I don't understand how clone() works. Shouldn't the return value equal the caller? int[] nums = new int[] {0, 1, 2}; int[] list = nums.clone(); nums.equals(list); //returns false. Why? for (int ket = 0; ket < list.length; ket++) { System.out.println(list[ket] == nums[ket]); //prints out true every time } list == nums //false 回答1: Because the equals implementation of array is the same as Object which is public boolean equals( Object o ) { return this == o; } See this also this question

Equality for anonymous types [duplicate]

南楼画角 提交于 2019-12-10 04:27:00
问题 This question already has answers here : Why does the Equals implementation for anonymous types compare fields? (4 answers) Closed 5 years ago . Why does the semantics of Equals() and == differs when used to compare anonymous types? Why does one compare values and the other compare references? What is the reason behind it? 回答1: == doesn't call Equals , it looks for == overloaded operator. Since anonymous types doesn't have overloaded == operator, so C# uses reference comparison for it. But