谁能以图解方式解释枚举如何存储在内存中? [英] Can anyone explain diagrammatically how enum is stored in memory?

查看:112
本文介绍了谁能以图解方式解释枚举如何存储在内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

枚举数据类型如何存储在内存中?枚举是否真的存储在堆栈中的内存中?如何访问枚举的每个值?

How Enumeration datatype is stored in memory?Is enum really stored in memory in stack?How each value of enum is accessed?

我浏览了许多博客,指出枚举没有存储在堆栈中.它们不需要内存来存储吗?

i have gone through many blogs stating that enum are not stored in stack.They don't require memory to store?

推荐答案

枚举是对编译器推断的数字数据类型的解释(通常是整数,但可以更改).

An enumeration is an interpretation of a numeric datatype inferred by the compiler (usually an integer, but you can change it).

编译器为您定义的每个枚举引入一种新类型.新定义的类型是特别的,因为您不能从其继承,并且包含可用于从其强制转换的运算符和整数类型.

The compiler introduces a new type for each enumeration you define. The newly defined type is particular, because you cannot inherit from, and contains operators that you can use to cast it from and an integral type.

从内存的角度来看,枚举存储在一个整数类型的值中,该值能够包含您声明的所有值.

From the memory perspective, the enumeration is stored in a value of integral type capable of containing all the values you have declared.

CLR将此作为基本服务提供,因此c#之类的语言具有此行为.这可能不是每个人的规则.网络支持的语言.

The CLR provides this as a base service, therefore languages like c# have this behavior. This might not be the rule for every. Net supported language.

由于枚举类型被处理为整数类型,它们被存储在堆栈中并作为各自的数据类型进行注册:标准枚举通常实现为int32;这意味着编译器会将您的枚举作为int32的同义词处理(为简单起见,我省略了一些细节).因此,您的枚举最后是一个int32.

As enum types are handled as integral types, they are stored in the stack and registers as their respective data types: a standard enum is usually implemented as an int32; this means that the compiler will handle your enum as a synonym of int32 (for the sake of simplicity, I left out several details). Hence your enum is at the end an int32.

如果您想控制枚举的实现方式,可以这样声明:

If you want to control how an enum is implemented, you can declare it this way:

enum MyEnum: byte{}

在上面的示例中,MyEnum是一个字节,因此编译器会将其作为字节处理.

In the example above, MyEnum is a byte, therefore the compiler will handle it as a byte.

这篇关于谁能以图解方式解释枚举如何存储在内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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