referenceequals

Object.Equals is virtual, but Object.operator== does not use it in C#?

99封情书 提交于 2019-12-23 10:20:08
问题 I got hit by a strange "asymmetry" in C# that I do not really understand. See the following code: using System; using System.Diagnostics; namespace EqualsExperiment { class Program { static void Main(string[] args) { object apple = "apple"; object orange = string.Format("{0}{1}", "ap", "ple"); Console.WriteLine("1"); Debug.Assert(apple.Equals(orange)); Console.WriteLine("2"); Debug.Assert(apple == orange); Console.WriteLine("3"); } } } It might be obvious for all you .NET gurus, but the 2nd

ReferenceEquals working wrong with strings

我的未来我决定 提交于 2019-12-17 16:13:25
问题 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

Why sometimes 2 objects reference the same but not always

六眼飞鱼酱① 提交于 2019-12-10 22:51:27
问题 Following the last answer : Recursive method to convert flat collection to hierarchal collection? I want to use the same method CreateTree but with another object than Hierarchy: ItemNode: public class ItemNode { public string Id { get; set; } public Item Item { get; set; } public ICollection<ItemNode> Children { get; set; } } and the definition of Item: public class Item { public string ID { get; set; } public string Name { get; set; } public int Level { get; set; } public string ParentId {

How use of == operator brings in performance improvements compared to equals?

倾然丶 夕夏残阳落幕 提交于 2019-12-06 03:58:36
问题 In Effective JAVA by Joshua Bloch, when I was reading about static factory methods , there was a statement as follows The ability of static factory methods to return the same object from repeated invocations allows classes to maintain strict control over what instances exist at any time. Classes that do this are said to be instance-controlled. There are several reasons to write instance-controlled classes. Instance control allows a class to guarantee that it is a singleton (Item 3) or

How use of == operator brings in performance improvements compared to equals?

痞子三分冷 提交于 2019-12-04 08:26:21
In Effective JAVA by Joshua Bloch, when I was reading about static factory methods , there was a statement as follows The ability of static factory methods to return the same object from repeated invocations allows classes to maintain strict control over what instances exist at any time. Classes that do this are said to be instance-controlled. There are several reasons to write instance-controlled classes. Instance control allows a class to guarantee that it is a singleton (Item 3) or noninstantiable (Item 4). Also, it allows an immutable class (Item 15) to make the guarantee that no two equal

Is the 'Is' VB.NET keyword the same as Object.ReferenceEquals?

梦想的初衷 提交于 2019-12-01 03:29:12
Is the Is VB.NET keyword the same as Object.ReferenceEquals? Yes, it is, unless combined with a TypeOf check. Quote from MSDN: The Is operator determines if two object references refer to the same object. However, it does not perform value comparisons. If object1 and object2 both refer to the exact same object instance, result is True; if they do not, result is False. Is can also be used with the TypeOf keyword to make a TypeOf...Is expression, which tests whether an object variable is compatible with a data type. BTW, also note the IsNot operator (which gives the boolean inverse of the

Is the 'Is' VB.NET keyword the same as Object.ReferenceEquals?

纵然是瞬间 提交于 2019-12-01 00:19:44
问题 Is the Is VB.NET keyword the same as Object.ReferenceEquals? 回答1: Yes, it is, unless combined with a TypeOf check. Quote from MSDN: The Is operator determines if two object references refer to the same object. However, it does not perform value comparisons. If object1 and object2 both refer to the exact same object instance, result is True; if they do not, result is False. Is can also be used with the TypeOf keyword to make a TypeOf...Is expression, which tests whether an object variable is

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

IEqualityComparer<T> that uses ReferenceEquals

醉酒当歌 提交于 2019-11-27 01:23:14
Is there a default IEqualityComparer<T> implementation that uses ReferenceEquals ? EqualityComparer<T>.Default uses ObjectComparer, which uses object.Equals() . In my case, the objects already implement IEquatable<T> , which I need to ignore and compare by object's reference only. Yurik Just in case there is no default implementation, this is my own: Edit by 280Z28: Rationale for using RuntimeHelpers.GetHashCode(object) , which many of you probably haven't seen before. :) This method has two effects that make it the correct call for this implementation: It returns 0 when the object is null.

IEqualityComparer<T> that uses ReferenceEquals

不羁岁月 提交于 2019-11-26 08:21:50
问题 Is there a default IEqualityComparer<T> implementation that uses ReferenceEquals ? EqualityComparer<T>.Default uses ObjectComparer, which uses object.Equals() . In my case, the objects already implement IEquatable<T> , which I need to ignore and compare by object\'s reference only. 回答1: Just in case there is no default implementation, this is my own: Edit by 280Z28: Rationale for using RuntimeHelpers.GetHashCode(object), which many of you probably haven't seen before. :) This method has two