equals

Which equality test does Ruby's Hash use when comparing keys?

余生长醉 提交于 2019-11-28 00:51:42
问题 I have a wrapper class around some objects that I want to use as keys in a Hash. The wrapped and unwrapper objects should map to the same key. A simple example will be this: class A attr_reader :x def initialize(inner) @inner=inner end def x; @inner.x; end def ==(other) @inner.x==other.x end end a = A.new(o) #o is just any object that allows o.x b = A.new(o) h = {a=>5} p h[a] #5 p h[b] #nil, should be 5 p h[o] #nil, should be 5 I've tried ==, ===, eq? and hash all to no avail. 回答1: Hash uses

Why does Json.Net call the Equals method on my objects when serializing?

故事扮演 提交于 2019-11-28 00:29:35
问题 I just ran into an error when I was using the Newtonsoft.Json SerializeObject method. It has been asked before here, but there was no answer from the people working with Newtonsoft as to why this happens. Basically, when calling SerializeObject like this: string json = Newtonsoft.Json.JsonConvert.SerializeObject(from, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All }); I get errors in a lot of Equals methods I have overridden in my classes: public override bool Equals

Where is the implementation of InternalEquals(object objA, object objB)

雨燕双飞 提交于 2019-11-28 00:16:30
While disassembling the .Net Source Code using Reflector, I came upon the Equals implementation in the Object Class and it refers to bool InternalEquals(object objA, object objB); Which again refers to internal static extern bool InternalEquals(object objA, object objB); I am now confused regarding where to find the implementation of this InternalEquals(object objA, object objB) function and how is it using this function and in which .Net assembly is this function defined and also if each and everything is written from scratch for the .Net Source Code, then why am I unable to find this

Difference between == operator and Equals() method in C#?

試著忘記壹切 提交于 2019-11-27 23:32:37
What is the difference between == and Equals() with example? I know that == is used to compare operator and Equals() method is used to compare content of string.So i tried // first example string s1 = "a"; string s2 = "a"; Console.Write(a.Equals(s2)); // returns true, but if I assign "b" to s2, // then result will be false // second example string s1 ="a"; string s2 ="a"; Console.Write(s1 == s2); // returns true How this is so? Both are different object references. Suppose we consider that these are reference. But I tried to use like this string s1 = new string("ab"); string s2 = new string(

Should we always override equals?

时光毁灭记忆、已成空白 提交于 2019-11-27 23:29:15
问题 When writing one's own classes, is it always necessary to override equals(Object o) ? If I don't, will it automatically check that all the fields are the same? Or does it just check if the two variables point to the same object? 回答1: If one is writing a class that is going to have its objects be compared in some way, then one should override the equals and hashCode methods. Not providing an explicit equals method will result in inheriting the behavior of the equals method from the superclass,

Comparing object used as Key in Dictionary

╄→гoц情女王★ 提交于 2019-11-27 23:15:21
问题 my class: public class myClass { public int A { get; set; } public int B { get; set; } public int C { get; set; } public int D { get; set; } } and main example: Dictionary<myClass, List<string>> dict = new Dictionary<myClass, List<string>>(); myClass first = new myClass(); first.A = 2; first.B = 3; myClass second = new myClass(); second.A = 2; second.B = 3; second.C = 5; second.D = 6; dict.Add(first, new List<string>()); if (dict.ContainsKey(second)) { // //should come here and update List

Two .NET objects that are equal don't say they are

假如想象 提交于 2019-11-27 22:56:12
I have the following code: object val1 = 1; object val2 = 1; bool result1 = (val1 == val2);//Equals false bool result2 = val1.Equals(val2); //Equals true What's up with that? Is the only way to fix this to go with .Equals() method? The operator == is static, not virtual, so the behaviour is determined by the static type and not the runtime type. The default implementation for == on objects of reference type is to compare the references (although types can implement a different behaviour, for example string ). You have two different objects and they don't have the same reference so == returns

Why are two AtomicIntegers never equal?

谁说我不能喝 提交于 2019-11-27 22:42:09
I stumbled across the source of AtomicInteger and realized that new AtomicInteger(0).equals(new AtomicInteger(0)) equals false . Why is this? Is it some "defensive" design choice related to concurrency issues? If so, what could go wrong if it was implemented differently? (I do realize I could use get and == instead.) Vineet Reynolds This is partly because an AtomicInteger is not a general purpose replacement for an Integer . The java.util.concurrent.atomic package summary states: Atomic classes are not general purpose replacements for java.lang.Integer and related classes. They do not define

Implementing equals and hashCode for objects with circular references in Java

做~自己de王妃 提交于 2019-11-27 22:41:27
I have two classes defined such that they both contain references to the other object. They look similar to this (this is simplified; in my real domain model class A contains a list of B and each B has a reference back to parent A): public class A { public B b; public String bKey; @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((b == null) ? 0 : b.hashCode()); result = prime * result + ((bKey == null) ? 0 : bKey.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return

ReferenceEquals working wrong with strings

╄→尐↘猪︶ㄣ 提交于 2019-11-27 22:26:48
Why in this situation ReferenceEquals method of object behaves differently? string a= "fg"; string b= "fg"; Console.WriteLine(object.ReferenceEquals(a, b)); So in this situation it's get a result true . In case, it compares values of my strings and not references. But when I write something like: StringBuilder c = new StringBuilder("fg"); string d = c.ToString(); Console.WriteLine(object.ReferenceEquals(a, d)); In this case it works fine and result is false , because it compares references of my objects. Anthony Pegram The first example has a compile time constant "fg" that is referenced by