Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET DateTime Does Not Have a Predefined 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.

like image 659
S. Valmont Avatar asked Jul 24 '11 00:07

S. Valmont


1 Answers

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 do I check the number of bytes consumed by a structure?

like image 187
Yahia Avatar answered Oct 15 '22 23:10

Yahia