简体   繁体   English

Object.ReferenceEquals对于匹配的字符串返回true

[英]Object.ReferenceEquals returns true for matching strings

I am using Mono and encountered an interesting result when comparing references of two strings. 我正在使用Mono,在比较两个字符串的引用时遇到了一个有趣的结果。 The code below demonstrates an example: 下面的代码演示了一个示例:

using System;

class Program
{
    static void Main()
    {
        String s1 = "asd";
        String s2 = "asd";
        Console.WriteLine("Reference Equals: {0}", Object.ReferenceEquals(s1, s2));

        Console.ReadLine();
    }
}

Yields true . 产量真实

It is interesting, two strings have same value but obviously they refer to two different instances. 有趣的是,两个字符串具有相同的值,但显然它们引用的是两个不同的实例。 What is going on? 到底是怎么回事?

mono --version : Mono JIT compiler version 3.2.6 OS X 10.9.2 mono --version:Mono JIT编译器版本3.2.6 OS X 10.9.2

http://msdn.microsoft.com/en-us/library/system.string.intern.aspx http://msdn.microsoft.com/en-us/library/system.string.intern.aspx

The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. 公共语言运行库通过维护一个称为内部池的表来保存字符串存储,该表包含对在程序中以编程方式声明或创建的每个唯一文字字符串的单个引用。 Consequently, an instance of a literal string with a particular value only exists once in the system. 因此,具有特定值的文字字符串的实例在系统中仅存在一次。

The below shows behavior when strings are not created from a string literal . 下面显示了不是从字符串文字创建字符串时的行为。

    static void Main(string[] args)
    {
        var string1 = new string(new []{'c'});
        var string2 = new string(new []{'c'});
        Console.WriteLine(string1.Equals(string2));                 //true
        Console.WriteLine(Object.ReferenceEquals(string1,string2)); //false
    }

It is interesting, two strings have same value but obviously they refer to two different instances 有趣的是,两个字符串具有相同的值,但显然它们引用的是两个不同的实例

no they dont refer to two different instances , infact there are no two different instances there is only one instance created as you are providing same string literal. 不,他们没有引用两个不同的实例 ,实际上, 没有两个不同的实例 ,因为您提供相同的字符串文字,所以创建了一个实例

in your program for all similar string constants one and only instance is created and all string reference variables refer the same instance hence when you run the ReferenceEquals() method on of those references , you will get True a they are logically referring same instance . 在您的程序中,为所有相似的string constants创建一个且只有一个instance ,并且所有字符串reference variablesreference variables同一instance因此,当您对这些references运行ReferenceEquals()方法时,您将得到True ,它们在逻辑上引用了同一instance

From : String Interning 来自: String Interning

The CLR maintains a table called the intern pool that contains a single, unique reference to every literal string that's either declared or created programmatically while your program's running. CLR维护一个称为“内部池”的表,该表包含对在程序运行时以编程方式声明或创建的每个文字字符串的唯一引用

if you want to see the expected result try the below snippet. 如果您想查看预期的结果,请尝试以下代码段。 it will create two different instances as they are passed to constructor. 当它们传递给构造函数时,它将创建两个不同的实例。

Try This: 尝试这个:

String s1 = new String("asd".ToCharArray());
String s2 = new String("asd".ToCharArray());
Console.WriteLine("Reference Equals: {0}",Object.ReferenceEquals(s1, s2));//False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 object.ReferenceEquals或==运算符? - object.ReferenceEquals or == operator? Object.ReferenceEquals为两个不同的对象输出true - Object.ReferenceEquals prints true for two different objects Assert.ReferenceEquals()在Visual Studio Test中Object.ReferenceEquals()返回'false'的位置 - Assert.ReferenceEquals() Passes where Object.ReferenceEquals() returns 'false' in Visual Studio Test Object.ReferenceEquals永远不会命中 - Object.ReferenceEquals never hit IEnumerable、Where 和 Object.ReferenceEquals 的问题 - Issue with IEnumerable, Where, and Object.ReferenceEquals 'Object.ReferenceEquals'始终为false,因为它使用值类型进行调用 - 'Object.ReferenceEquals' is always false because it is called with a value type c#中Object.Equals(object,object)和Object.ReferenceEquals(object,object)之间的区别 - difference between Object.Equals(object,object) and Object.ReferenceEquals(object,object) in c# C#operator ==,StringBuilder.Equals,Object.Equals和Object.ReferenceEquals之间的差异 - C# Differences between operator ==, StringBuilder.Equals, Object.Equals and Object.ReferenceEquals 为什么我要在Equals覆盖中执行object.ReferenceEquals(null,this)? - Why would I ever want to do object.ReferenceEquals(null, this) in Equals override? Reference等于字符串 - ReferenceEquals on strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM