字符串是与数组相同的引用类型.它是引用类型吗? [英] string is a reference type as same as array..is it reference type?

查看:89
本文介绍了字符串是与数组相同的引用类型.它是引用类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class str
    {
        string s1 = "111";
        int[] ar1 = new int[1];

            public void add()
        {
         //   string s2 = s1;
            int[] ar2 = new int[1];
            ar2 = ar1;
            ar1[0] = 4;

            Console.WriteLine(ar1[0]);
            Console.WriteLine(ar2[0]);
            //s2 = "222";
                ar2[0] = 5;
            Console.WriteLine(ar1[0]);
            Console.WriteLine(ar2[0]);
        }
    }





在这个例子中,如果我们将数组的值赋给第二个数组,那么它也在第一个数组中被更新.因为它是引用类型.但是如果我们在字符串中进行操作,那么它就无法工作....为什么??





in this example if we are giving the array value to second array,it is getting updated in first array also.Because it is reference type.But if we are doing in string,it is not working ....Why??

推荐答案

因为字符串是不可变的,即它们是只读的并且它们的值不能被修改.每次对字符串进行操作时,都会创建一个新的字符串对象.

有关更多信息,请检查MSDN链接-字符串类 [^ ]并向下滚动至不变性.

阅读本文,您将获得有关String对象的非常清晰的图片.
Because strings are immutable i.e they are read-only and their values cannot be modified. Every time you do an operation on a string, a new String Object is created.

For more information check the MSDN link - String Class[^] and scroll down to immutability.

Read the article and you will have a very clear picture about String objects.


.NET中的String是引用类型,但它是不可变的类型,这意味着它不能更改一旦创建.

String in .NET is a reference type but it is an immutable type, meaning that it can''t be changed once it is created.

string s = string.Empty;
s = "Hello, World";
s = "Hello, " + "World";



在这种情况下,将为s创建三个新的字符串对象.

http://msdn.microsoft.com/en-us/library/362314fe.aspx [ ^ ]



In this case three new string objects will be created for s.

http://msdn.microsoft.com/en-us/library/362314fe.aspx[^]


解释起来并不简单,但我会尝试:
您需要知道的第一件事是字符串是immutable-当您声明一个字符串(例如"hello there")时,它不能被更改,甚至不能被更改.尝试时(通过添加"Samu!"来说),结果是系统将两个字符串的长度相加,为结果分配足够的内存,然后将两者都复制到其中,并返回新的字符串值-原始字符串不会以任何方式更改.
另一件事是,当您声明一个数组时:
It''s not simple to explain, but I''ll try:
The first thing you need to know is that strings are immutable - when you declare a string (say "hello there") it cannot be changed, not even slightly. When you try, (Say by adding " Samu!") what happens is that the system adds the lengths of the two strings, allocates enough memory for the result, and copies both of then into it, returning the new string value - the original string is not changed in any way.
The other thing is that when you declare an array:
int[] ar1 = new int[1];


您已完成两项操作:
1)您已声明一个名为ar1的变量,该变量可以引用一个整数数组.
2)您构造了一个新的整数数组,并将其引用分配给ar1

这意味着当您随后声明另一个变量时:


You have done two operations:
1) You have decalared a variable called ar1 which can reference an array of ints.
2) You have constructed a new array of integers and assigned it''s reference to ar1

What that means is that when you subsequently declare another variable:

int[] ar2 = new int[1];
ar2 = ar1;

您首先为其分配对新数组的引用,然后用ar1所引用的引用覆盖该引用. ar1ar2现在都引用内存中的同一数组.

因此,当您通过ar1更改数组内容时,它会影响ar2所引用的内存.

当您对字符串执行同一件事"时,所有事情都会发生相同的事情:

You first assign a reference to a new array to it, then overwrite that reference with the one that ar1 is referring to. ar1 and ar2 now both reference that same array in memory.

So when you change the array content via ar1 it affects the memory that ar2 is also refering to.

When you do "the same thing" with strings, it all happens the same:

string s1 = "111";

声明一个字符串变量s1,并将其引用分配给持有" 111的内存.

Declares a string variable s1, and assigns it a reference to the memory holding "111".

string s2 = s1;

声明字符串变量s2,并从s1复制引用.现在,两个变量引用内存中的同一字符串:"111"

Declares a string variable s2, and copies the reference from s1. The two variables now refer to the same string in memory: "111"

s2 = "222";

更改s2到内存中的新字符串.它不会以任何方式影响s1-s1继续引用持有"111"的内存

Changes the reference of s2 to a new string in memory. It does not affect s1 in any way - s1 continues to refer to the memory holding "111"


这篇关于字符串是与数组相同的引用类型.它是引用类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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