如何确定一个数据类型有多大是.NET? [英] How do I determine how big a data type is in .NET?
问题描述
我想加深我的数据类型的了解,想知道我怎么能确定一个数据类型是有多大。我希望旅途的答案揭示未知其他
I would like to deepen my understanding of data types and want to know how I can determine how big a data type is. I'm hoping the journey to the answer uncovers other unknowns.
我知道,在.NET中,一个字节是一个8位无符号整数,但只因为我读过它。如果我有像下面这样一个非常简单的控制台应用程序:
I know that in .NET, a byte is an 8-bit unsigned integer, but only because I've read about it. If I have a very simple console app like the following:
static void Main(string[] args)
{
byte b = 1;
}
我如何知道在Visual Studio中,有多大'b的数据结构是什么?我看到有一些内存诊断窗口,但它只是说他们无法当我跨过B的分配来计算表达式。我有反汇编窗口打开,可以看到MOV运...
How can I tell in Visual Studio, how big the data structure of 'b' is? I see that there are some Memory diagnostic windows but it only says that they're unable to evaluate the expression when I step over the assignment of b. I have the Disassembly window open and can see the mov op...
mov dword ptr [ebp-40h],1
...但不知道什么是信息化手段。我在想,DWORD是在暗示大小,也许PTR EBP-40H指向的地址。有没有一种方法,我可以看到所有的地址在Visual Studio中,或许通过观察范围内收集的大小?
... but not sure what the information means. I'm thinking that dword is hinting at the size and perhaps ptr ebp-40h is pointing to an address. Is there a way I can see all of the addresses in Visual Studio and perhaps glean the size by looking at the range?
我知道这些都是棘手的问题的回答短论坛这样的,但感谢您的帮助。
I know these are tough questions to answer in a short forum like this but thanks for any help.
推荐答案
您可以使用的sizeof(T)
来获得原始值类型和非引用类型的大小。对于其他类型的,它可以是很难获得的大小,因为引用类型通常不具有固定的大小。
You can use sizeof(T)
to get the size of primitive value types and non-reference types. For other types, it can be very difficult to obtain the size, because reference types generally do not have a fixed size.
这可以是简单的类型之外的复杂的问题(每下面的讨论),但通常,如果一个类型的大小不被它的数据结构明确地限定,因为它本身是一个参考的类型,或者它是一个包含引用类型,则没有办法来计算存储(因为它是根据定义,不固定)的结构。
This can be a complex issue outside simple types (per the discussion below), but generally, if the size of a type is not defined explicitly by its data structures because it is itself a reference type, or it is a structure that contains reference types, then there is no way to calculate storage (since it is, by definition, not fixed).
这篇关于如何确定一个数据类型有多大是.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!