什么数据将存储在C#中的堆栈中 [英] what data will be stored in stack in C#

查看:151
本文介绍了什么数据将存储在C#中的堆栈中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,所有类变量都类似于实例成员,静态成员,常量s ...,它们的引用将保存在堆中,而这些变量的实际值将保存在堆栈中。

As my understanding all the class variable's like instance members , static members , constant s... their references will be saved in heap, and actual value of these variables will be saved in stack.

如果它只是值类型而不是任何引用类型都不引用,则它将像枚举一样存储在堆栈中,除了枚举之外,这些数据也将存储在堆栈存储器中。

if it is only value type not refer by any reference type it will be stored in stack only like enum, apart from enum which data will be stored in stack memory.

如果我的理解有误,请纠正我。

if my understanding is wrong correct me.

推荐答案

参考类型总是在堆上分配,值类型可以在栈上分配-但不一定。仅当值类型是未装箱的,未转义的局部或临时变量且未包含在引用类型中且未在寄存器中分配时,才在堆栈上分配值类型。

While reference types are always allocated on the heap, value types may be allocated on the stack - but not necessarily. A value type is only allocated on the stack when it is an unboxed non-escaping local or temporary variable that is not contained within a reference type and not allocated in a register.

堆栈中不会包含的内容:

What will NOT en up in the stack:


  • 作为类的一部分的值类型将最终出现在堆中。

  • 如果装箱,它将结束在堆上。

  • 如果是数组,将结束在堆上。

  • 如果它是一个静态变量,它将最终在堆上。

  • 如果被闭包捕获,它将最终在堆上。

  • 如果在迭代器或异步块中使用,它将最终在堆上。

  • 如果由不安全或不受管理的代码创建,则可以在任何类型的数据结构中分配它(

  • a value type as part of a class will end up on the heap.
  • if boxed, it will end up on the heap.
  • if an array, it will end up on the heap.
  • if it's a static variable, it will end up on the heap.
  • if captured by a closure, it will end up on the heap.
  • If used in an iterator or async block, it will end up on the heap.
  • If created by unsafe or unmanaged code, it could be allocated in any type of data structure (not necessarily a stack or a heap).

枚举仅在未作为部分分配时才会分配在堆栈中其他结构,如数组。

Enums will only be "allocated" in the stack when they're not allocated as part of some other structure, like an array.

这篇关于什么数据将存储在C#中的堆栈中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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