Variable Declaration & Memory Allocation

前端 未结 2 1532
轮回少年
轮回少年 2021-01-28 15:41

I want to know whether memory is allocated during the local variable declaration process.

Suppose I write this code inside function, int a =10; memory i

2条回答
  •  别那么骄傲
    2021-01-28 16:35

    When you call a method, space for each local variable is allocated on the stack.

    So if you declare an int variable in a method, it's stack frame will take up an extra 4 bytes of memory.

    No additional memory is used anywhere else, and it is cleaned up when the method returns.

    Something important to understand here is that MSIL does not support declaring a property just anywhere in a method. Whenever you declare a variable in C#, the declaration is moved to the method header in the compiled bytecode. Every variable is allocated when the method is called.

提交回复
热议问题