这是一个可为空&所述的大小;的Int32&GT ;? [英] What is the size of a Nullable<Int32>?

查看:120
本文介绍了这是一个可为空&所述的大小;的Int32&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

于是,一对夫妇的问题,实际上是:

  1. INT 的Int32 )被指定为(显然)32位。怎么样一个 INT 可空< INT> )?我的直觉告诉我,这将是32位整数加上8个位布尔,但也许是实现比这更复杂。
  2. 我会回答用我自己的问题的sizeof(INT?);但作为诠释?是托管型,这是不允许的。据我所知,一个类型的大小可能是依赖于平台的,而且其中包含对其他对象的对象的情况下,的sizeof 式的操作会产生误导。但是,有没有办法让一个基准的大小(即,什么是新实例化实例的大小会)的托管类型,考虑到目前的环境?
解决方案

您可以在反汇编一看或反思。

如果有两个字段:布尔 T ,所以大概8个字节(假设4字节对齐)

So, a couple of questions, actually:

  1. An int (Int32) is specified to be (obviously) 32 bits. What about an int? (Nullable<int>)? My gut tells me that it would be 32 bits for the integer plus 8 more bits for the boolean, but perhaps the implementation is more intricate than that.
  2. I would have answered my own question using sizeof(int?); but as int? is a managed type this is not allowed. I understand that the size of a type may be platform-dependent, and that in the case of objects which contain references to other objects, a sizeof-like operation would be misleading. However, is there a way to get a "baseline" size (i.e., what the size of a newly instantiated instance would be) for a managed type, given the current environment?

解决方案

You can take a look in ildasm or Reflector.

If has two fields: a bool and a T, so probably 8 bytes (assuming 4 byte alignment).

这篇关于这是一个可为空&所述的大小;的Int32&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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