为什么结构中的引用类型的行为类似于值类型? [英] Why reference types inside structs behave like value types?

查看:156
本文介绍了为什么结构中的引用类型的行为类似于值类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#编程的初学者.我现在正在学习stringsstructsvalue typesreference types.作为中的已接受答案此处strings是引用类型,其指针存储在堆栈中,而其实际内容存储在堆中.另外,如此处所述,structs是值类型.现在,我举一个小例子尝试使用structsstrings:

I am a beginner to C# programming. I am now studying strings, structs, value types and reference types. As accepted answers in here and in here, strings are reference types that have pointers stored on stack while their actual contents stored on heap. Also, as claimed in here, structs are value types. Now I try to practice with structs and strings with a small example:

struct Person
{
    public string name;
}

class Program
{
    static void Main(string[] args)
    {
        Person person_1 = new Person();
        person_1.name = "Person 1";

        Person person_2 = person_1;
        person_2.name = "Person 2";

        Console.WriteLine(person_1.name);
        Console.WriteLine(person_2.name);
    }
}

上面的代码片段输出

Person 1
Person 2

这让我感到困惑.如果strings是引用类型,而structs是值类型,那么person_1.name和person_2.name应该指向堆上的同一空间区域,不是吗?

that makes me confused. If strings are reference types and structs are value types then person_1.name and person_2.name should point to the same space region on heap, shouldn't them?

推荐答案

字符串是引用类型,其指针存储在堆栈中,而其实际内容存储在堆中

strings are reference types that have pointers stored on stack while their actual contents stored on heap

不,不,不.首先,停止考虑堆栈和堆.在C#中,这几乎总是错误的思维方式. C#为您管理存储寿命.

No no no. First off, stop thinking about stack and heap. This is almost always the wrong way to think in C#. C# manages storage lifetime for you.

第二,尽管引用可以实现为指针,但是引用在逻辑上不是指针.参考是参考. C#同时具有引用和指针.不要把它们混在一起.从来没有C#中的字符串指针.有对字符串的引用.

Second, though references may be implemented as pointers, references are not logically pointers. References are references. C# has both references and pointers. Don't mix them up. There is no pointer to string in C#, ever. There are references to string.

第三,对字符串的引用可以存储在堆栈中,但是也可以存储在堆中.当您有一个对字符串的引用数组时,该数组的内容就在堆上.

Third, a reference to a string could be stored on the stack but it could also be stored on the heap. When you have an array of references to string, the array contents are on the heap.

现在让我们来解决您的实际问题.

Now let's come to your actual question.

    Person person_1 = new Person();
    person_1.name = "Person 1";
    Person person_2 = person_1; // This is the interesting line
    person_2.name = "Person 2";

让我们说明代码在逻辑上的作用.您的Person结构只不过是一个字符串引用,因此您的程序与以下内容相同:

Let's illustrate what the code does logically. Your Person struct is nothing more than a string reference, so your program is the same as:

string person_1_name = null; // That's what new does on a struct
person_1_name = "Person 1";
string person_2_name = person_1_name; // Now they refer to the same string
person_2_name = "Person 2"; // And now they refer to different strings

当您说person2 = person1时,这并不意味着变量person1现在是变量person2的别名. (在C#中有一种方法可以做到这一点,但事实并非如此.)这意味着将person1的内容复制到person2".对该字符串的引用是要复制的值.

When you say person2 = person1 that does not mean that the variable person1 is now an alias for the variable person2. (There is a way to do that in C#, but this is not it.) It means "copy the contents of person1 to person2". The reference to the string is the value that is copied.

如果不清楚,请尝试为变量绘制图形框,并为引用绘制箭头;复制该结构时,将复制箭头,而不复制 box .

If that's not clear try drawing boxes for variables and arrows for references; when the struct is copied, a copy of the arrow is made, not a copy of the box.

这篇关于为什么结构中的引用类型的行为类似于值类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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