initialization - Does declaration in c# allocate memory or is it the new operator that allocates memory? -
does declaration in c# allocate memory variable created or new operator allocates memory , enables invoke constructor initialize allocated variable in memory?
to understanding, cannot call constructor of type without new operator. correct?
does declaration in c# allocate memory variable created or new operator allocates memory , enables invoke instructor initialize allocated variable in memory?
first let's make sure you're asking question think you're asking. value type, variable storage location , value storage location the same storage. reference type, storage location associated the variable contains reference storage location associated the object. different.
second, let's clarify mean "declaration" of "variable". static field, instance field, local variable , formal parameter have declarations. moreover, allocation semantics of local variables , formal parameters different if closed-over outer locals of lambda, , semantics different when local in async method or iterator block.
so let's assume have local variable of reference type , there nothing fancy local:
void m() { animal x = new giraffe(); ...
the storage location local variable x allocated off of short-term storage -- stack, or register, typically -- when method m() activated.
when "new giraffe()" evaluated runtime allocates memory giraffe on long-term storage -- gc heap -- , passes reference object constructor. when constructor returns, reference assigned local.
so there two storage locations. there's short-term location x, lives long activation of method, , there's long-term storage thing referred to, , lives until garbage collector cleans up.
if doesn't answer question, clarify question.
can call constructor without new operator?
i'm assuming "constructor" mean instance constructor , not static constructor.
not "normal" means, no.
Comments
Post a Comment