简体   繁体   中英

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

everyone... I'm new to C# and such languages...

I've read two articles by Skeet, one about heap/stack , and other about reference types and value types . 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?

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...

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. 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.

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.

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

No. Passing a Form reference to a function is no more expensive than passing an integer.

A Form is a large object (probably tens of KB when you add everything up). 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.

No, only a reference is allocated.

In C# an instance of a reference type can only be passed by reference. 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 . 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.

If the method parameter was struct or a primitive type like an int, then it would be a value type. Value types, when passed into methods, have all of their data copied into the stack frame of the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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