简体   繁体   English

C#引用类型在传递给方法时是否分配新内存?

[英]Do C# reference types allocates new memory when passed to methods?

everyone... I'm new to C# and such languages... 大家......我是C#和其他语言的新手......

I've read two articles by Skeet, one about heap/stack , and other about reference types and value types . 我读过Skeet的两篇文章,一篇关于堆/堆栈 ,另一篇关于引用类型和值类型 And I assume my question is simple, but it's not clarified to me after reading those articles. 而且我认为我的问题很简单,但在阅读这些文章后我并未澄清。

Do reference types allocate new memory when passed to methods? 传递给方法时引用类型是否分配新内存?

For example, if I pass a Form to a method, like 例如,如果我将表单传递给方法,例如

void myMethod(System.Windows.Forms.Form myForm)
{
...
}

Will there be more memory allocated to store all myForm data or will it hold just a reference to where myForm data is already allocated? 是否会分配更多的内存来存储所有myForm数据,还是仅仅包含对myForm数据已分配位置的引用?

My worry is that if there's more memory allocated to store everything "appended" to myForm , soon the memory could become full, if myForm were a big form... 我担心的是,如果分配更多的内存来存储“附加”到myForm所有内容,很快内存就会变满,如果myForm是一个很大的形式......

When you instantiate your original form, the form object will allocated on the heap. 实例化原始表单时,表单对象将在堆上分配。 When you call myMethod, you're passing a reference type (essentially a pointer to that object) by value. 当您调用myMethod时,您将按值传递引用类型(本质上是指向该对象的指针)。 That means that the reference you're passing in will be copied in the context of myMethod - this involves a 32/64 bit stack allocation, depending on your architecture. 这意味着您传入的引用将在myMethod的上下文中复制 - 这涉及32/64位堆栈分配,具体取决于您的体系结构。

So, to answer your question, your form will not be copied but the reference to it will be. 因此,要回答您的问题,您的表单将不会被复制,但对其的引用将是。

Disclaimer: I haven't used C# in ages. 免责声明:我很久没有使用过C#了。

从技术上讲,它们将创建引用的副本(因此另一个“引用大小的”内存分配),但引用的实际对象将不会被复制和重新分配。

No. Passing a Form reference to a function is no more expensive than passing an integer. Form引用传递给函数并不比传递整数更昂贵。

A Form is a large object (probably tens of KB when you add everything up). Form是一个大对象(当你添加所有东西时可能是几十KB)。 Only a reference to it can be passed as a parameter. 只有对它的引用才能作为参数传递。 This reference is only 4 bytes (in a 32-bit process) and vanishes when the method returns. 此引用仅为4个字节(在32位进程中),并在方法返回时消失。

No, only a reference is allocated. 不,仅分配参考。

In C# an instance of a reference type can only be passed by reference. 在C#中,引用类型的实例只能通过引用传递。 That is, the object itself is never copied, only the reference to it is. 也就是说,对象本身永远不会被复制,只有对它的引用。
Strictly speaking, when you pass an object to a method, its reference is passed by value . 严格地说,当您将对象传递给方法时,其引用将按传递。 Which means, you can access the object using this reference, and changes you make to the object will be visible to the code that called your method, but, if you make that received reference point to some other object, that change will not affect the code that gave you the original reference -- because the reference was passed by value. 这意味着,您可以使用此引用访问该对象,并且您对该对象所做的更改将对调用您的方法的代码可见,但是,如果您将该接收的引用指向某个其他对象,则该更改不会影响为您提供原始引用的代码 - 因为引用是按值传递的。

As opposed to value types that, when passed by value, do get copied. 与值类型相反,当按值传递时,会复制。

A Form is a class. 表格是一个类。 In C# a class is a reference type - which means that when it passed around it is passed by reference . 在C#中,一个类是一个引用类型 - 这意味着当它传递时它通过引用传递。 This basically means that the address of the memory where the Form instance resides is passed into the stack frame instead of copying all of the bytes. 这基本上意味着Form实例所在的内存地址被传递到堆栈帧而不是复制所有字节。

If the method parameter was struct or a primitive type like an int, then it would be a value type. 如果方法参数是struct或类似int的基本类型,那么它将是值类型。 Value types, when passed into methods, have all of their data copied into the stack frame of the method. 传递给方法时,值类型将其所有数据复制到方法的堆栈帧中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM