String是否真的是引用类型 [英] Is String really Reference Type

查看:66
本文介绍了String是否真的是引用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我研究过String是一种引用类型而不是值类型。如果我错了请纠正我

但我在做同等检查时看到了行为上的差异。



I have studied String is a reference type not a value type. Please correct me if I m wrong
But I saw a difference in behavior while doing equality check.

Main()
{
	 String check1 = new String(new char[] {'s','i','d'});
            String check2 = new String(new char[] { 's', 'i', 'd' });

            if(check1 == check2)
            {
                Console.WriteLine("I m Similar");
            }

            Panda newPanda3 = new Panda("Sid");
            Panda newPanda4 = new Panda("Sid");

            if(newPanda3 == newPanda4)
            {
                Console.WriteLine("I m Similar Pandas");
            }
           
}

public class Panda
    {
        public string Name;
        public static int Population;

        public Panda(string name)
        {
            Name = name;
            Population = Population + 1;
        }
    }





在上面的场景中,当我检查两个字符串对象时,输出为真

但是当我检查两个Panda对象时,输出是错误的。



如果string是引用类型,那么两个对象怎么可能是平等的。请让我知道您的观点。



In the above scenario when I check two string objects, the output is true
But the same when I check two Panda objects, the output is false.

If string is a reference type, then how could both the objects is equal. Please let me know your views.

推荐答案

是的,它肯定是一种参考类型。你可以看到它,如果你拿对象 System.Type stringType = string.GetType()并检查它。



但是这种类型的实现非常不寻常。基本上,它模拟值类型语义。首先,字符串的语义内容是 immutable 。没有办法修改字符串内容。修改内容的所有字符串函数实际上创建了一个全新的字符串对象并将其返回。此外,它基于实习机制和实习池。请参阅:

http: //msdn.microsoft.com/en-us/library/system.string.intern(v=vs.110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system .string.isinterned(v = vs.110).aspx [ ^ ],

http://en.wikipedia.org/wiki/String_interning [ ^ ]。



最后,你关于平等的问题太过微不足道了。您可以在自己的类型上创建相同的效果。您需要覆盖 System.Object.Equals 方法,该方法还需要覆盖 System.Object.GetHashCode (执行此规则的原因非常明显,与基于 buckets 的集合中的对象的使用有关,基于哈希代码的机制):

http://msdn.microsoft.com/en-us/library/system.object %28v = vs.110%29.aspx [ ^ ]。



此外,您可以定义自定义==和!=运算符。这样,您可以定义自己的等价标准,而不是参考但是语义。



-SA
Yes, it is surely a reference type. You can see it if you take the object System.Type stringType = string.GetType() and inspect it.

But the implementation of this type is pretty unusual. Basically, it simulates value-type semantic. First of all, the semantic content of the string is immutable. There is no a way to modify string content. All string functions modifying content actually create a brand-new string object and return it. Besides, it is based on the interning mechanism and intern pool. Please see:
http://msdn.microsoft.com/en-us/library/system.string.intern(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.string.isinterned(v=vs.110).aspx[^],
http://en.wikipedia.org/wiki/String_interning[^].

And, finally, your question about equality is way too trivial. You can create the same effect on your own types. You need to override System.Object.Equals method, which will also require overriding of System.Object.GetHashCode (the reasons for enforcing this rule are pretty obvious, related to the use of the object in collections based on buckets, the mechanism based on hash code):
http://msdn.microsoft.com/en-us/library/system.object%28v=vs.110%29.aspx[^].

Also, you can define your custom == and != operators. This way, you can define your own equivalence criteria, not referential but semantic.

—SA


这篇关于String是否真的是引用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆