.NET DateTime没有预定义的大小 [英] .NET DateTime Does Not Have a Predefined Size

查看:237
本文介绍了.NET DateTime没有预定义的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为DateTime是一个结构,成员似乎分解成简单的数学值,我不知道为什么使用sizeof()在问题标题中生成消息。

解决方案

因为CLR只能在运行时确定大小...其中一个原因是padding(依赖平台)...


对于所有其他类型(包括结构体),sizeof运算符只能在不安全的代码块中使用
。虽然可以使用
Marshal.SizeOf方法,但此方法返回的值并不总是与$ size返回值相同的
。 Marshal.SizeOf在类型被封送后返回
大小,而sizeof返回
size ,因为它已由公共语言运行时分配,
包括任何填充


参考



另请参阅如何检查我的结构消耗的字节数?


Since DateTime is a struct with members that appear to break down into simple mathematical values, I'm not sure why using sizeof() on it produces the message in the question title.

解决方案

Because the CLR can only determine the size at runtime... one of the reasons for this is "padding" (platform dependent)...

For all other types, including structs, the sizeof operator can be used only in unsafe code blocks. Although you can use the Marshal.SizeOf method, the value returned by this method is not always the same as the value returned by sizeof. Marshal.SizeOf returns the size after the type has been marshaled, whereas sizeof returns the size as it has been allocated by the common language runtime, including any padding.

Ref.

see also How to check the number of bytes consumed by my Structure?

这篇关于.NET DateTime没有预定义的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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