在C Sharp中堆叠和堆放 [英] Stack and heap in c sharp

查看:73
本文介绍了在C Sharp中堆叠和堆放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
为什么类存储在堆(.NET)上时存储在堆栈上的结构?

Possible Duplicate:
Why are structs stored on the stack while classes get stored on the heap(.NET)?

谁能告诉我,哪个对象要存储在堆栈中,哪个对象要在内存的堆部分中完成内存分配?

Can anyone tell me that how the allocation of memory is done that which object is to be stored in stack and which to be in heap portion of the memory?

推荐答案

3条经验法则:

  1. 对象存储在堆中.这些包括引用类型和装箱的值类型的实例.
  2. 局部变量和参数存储在堆栈中.对于局部值类型,这意味着值本身存储在堆栈中.对于局部引用类型,只有 reference 会在堆栈上(Eric Lippert指出的异常-值类型局部变量在外部变量(迭代器块值类型)上封闭).
  3. 字段存储在包含实例的位置.例如,一个类的值类型字段将存储在堆中.声明为本地存储的结构的引用类型字段的引用部分也将位于堆栈上.
  1. Objects are stored on the heap. These include instances of reference-types and boxed value-types.
  2. Local variables and parameters are stored on the stack. For local value-types, this means that the value itself is stored on the stack. For local reference-types, only the reference will be on the stack ( Exceptions noted by Eric Lippert - value-type locals closed over outer variables, iterator-block value-types).
  3. Fields are stored where the containing instance is located. For example, a value-type field of a class will be stored on the heap. The reference-part of a reference-type field of a struct declared as a local stored on the stack will also be on the stack.

这篇关于在C Sharp中堆叠和堆放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆