C#字符串引用类型作为副本传递? [英] C# String reference type passed as copy?

查看:220
本文介绍了C#字符串引用类型作为副本传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#字符串"引用类型有疑问.

I have this doubt related to C# "String" reference types .

以下代码:

string s = "lana del rey" 
string d = s;
s = "elvis presley";
Console.Writeline(d);

为什么输出结果不是"elvis presley"?如果d指向s的相同存储位置?

Why the Output is not "elvis presley" ? If d is pointing to the same memory location of s ?

您能解释一下吗?

我最初的问题的更详细的解释:

A more detailed explanation of my initial question:

您的所有答案都非常有用. 这个问题是通过常见的代码示例(通常用于解释值类型和引用类型之间的差异)出现的:

All your answers were very useful. That question came to me with this common code sample frecuently used to explain difference between value types and reference types:

class Rectangle
{
    public double Length { get; set; }
}

struct Point 
{
    public double X, Y;
}

Point p1 = new Point();
p1.X = 10;
p1.Y = 20;
Point p2 = p1;
p2.X = 100;
Console.WriteLine("p1.X = {0}", p1.X);

Rectangle rect1 = new Rectangle
{ Length = 10.0, Width = 20.0 };
Rectangle rect2 = rect1;
rect2.Length = 100.0;
Console.WriteLine("rect1.Length = {0}",rect1.Length);

在这种情况下,第二个Console.WriteLine语句将输出:"rect1.Length = 100"

In this case, the second Console.WriteLine statement will output: "rect1.Length = 100"

在这种情况下,类是引用类型,而struct是值类型.如何使用字符串演示相同的引用类型行为?

In this case class is reference type, struct is value type. How can I demostrate the same reference type behaviour using a string ?

谢谢.

推荐答案

它与可变性没有任何关系

It has nothing to do with mutability

string s = "lana del rey" 
string d = s;

这里有2个变量sd指向内存中的同一对象.

Here 2 variables s and d refer to the same object in memory.

s = "elvis presley";

在语句的右侧,使用"elvis presley"分配和初始化新对象,并将其分配给s.因此,现在s引用了另一个对象.而且,尽管我们没有更改d引用值-仍然像原来一样继续引用"lana del rey".

here in the right part of the statement the new object is allocated and initialized with "elvis presley" and assigned to s. So now s refers to another object. And while we haven't change the d reference value - it continues referring to the "lana del rey" as it originally did.

现在现实生活中的比喻:

Now the real life analogy:

有2个人(AB)用他们的手指指着远处的建筑物.它们彼此独立,甚至看不到另一个指向的对象.然后A决定开始指向另一座建筑物.只要他们没有彼此连接-现在A指向另一座建筑物,并且B继续指向原始建筑物(因为没有人要求他们停止这样做)

There are 2 people (A and B) pointing using their fingers to a building far away. They are independent to each other, and don't even see what another is pointing to. Then A decides to start pointing to another building. As long as they aren't connected to each other - now A points to another building, and B continues pointing to the original building (since no one asked them to stop doing that)

PS:您可能感到困惑的是指针和引用背后的概念.不知道在这里进行解释是否有意义,因为您可能会更加困惑.但是现在至少您可以在Google上搜索相应的关键字.

PS: what you probably are confusing is the concept behind a pointer and a reference. Not sure if it makes sense to explain it here since you might be confused even more. But now at least you might google for the corresponding keywords.

这篇关于C#字符串引用类型作为副本传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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