Does declaration in c# allocate memory or is it the new operator that allocates memory?

风流意气都作罢 提交于 2019-12-01 07:54:31

问题


Does declaration in c# allocate memory for the variable to be created or is it the new operator that allocates memory and enables to invoke the constructor to initialize the allocated variable in memory?

To my understanding, you cannot call an constructor of any type without the new operator. Am I correct?


回答1:


Does declaration in c# allocate memory for the variable to be created or is it the new operator that allocates memory and enables to invoke the instructor to initialize the allocated variable in memory?

First let's make sure that you're asking the question you think you're asking. For a value type, the variable storage location and the value storage location are the same storage. For a reference type, the storage location associated with the variable contains a reference to the storage location associated with the object. Those are completely different.

Second, let's clarify what you mean by "declaration" of a "variable". A static field, an instance field, a local variable and a formal parameter all have declarations. Moreover, the allocation semantics of local variables and formal parameters are different if they are closed-over outer locals of a lambda, and the semantics are also different when the local is in an async method or iterator block.

So let's assume that you have a local variable of reference type and there is nothing fancy about the local:

void M() { 
    Animal x = new Giraffe();  ...

The storage location for the local variable x is allocated off of the short-term storage -- the stack, or a register, typically -- when method M() is activated.

When the "new Giraffe()" is evaluated the runtime allocates memory for a Giraffe on the long-term storage -- the GC heap -- and then passes a reference to that object to the constructor. When the constructor returns, the reference is then assigned to the local.

So there are two storage locations. There's the short-term location for x, which only lives as long as the activation of the method, and there's the long-term storage for the thing that is referred to, and that lives until the garbage collector cleans it up.

If that doesn't answer your question, clarify your question.

Can you call a constructor without the new operator?

I'm assuming that by "constructor" you mean an instance constructor and not a static constructor.

Not by any "normal" means, no.




回答2:


declaration without specification allocates memory for (object)null in C#...

string x;
// x = null in memory

string x = "";
// x = string in memory with value.

MyObjectType x;
// x = null;

MyObjectType x = new MyObjectType();
// x = MyObjectType in memory.



回答3:


Declaration of Reference types takes only space for the reference, until you create an object with new keyword. Same is for Value types

so if you have:

string s; // only reference type memory, which is based on the machine architecture.

if you do have:

string s = "some string"; // reference (pointer) + memory allocated on the heap.

It is almost the same for Value types, though, their memory is allocated inline where the value type is declared.

And you're correct, you cannot call constructor on any type unless an object of the type is is instantiated / created.



来源:https://stackoverflow.com/questions/15824971/does-declaration-in-c-sharp-allocate-memory-or-is-it-the-new-operator-that-alloc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!