Mark Rogers Mark Rogers - 4 months ago 35
C# Question

What is the average size of a DateTimeOffset on a x64 bit Azure machine?

I've been trying to get the size of a

struct in my code so that I can compute the size of a parent object. The problem is that neither the
sizeof
operator nor the
Marshal.SizeOf
function work for this purpose.

sizeof won't work because I have to compile with the unsafe flag, and this feature isn't enough of a justification to do that. Marshal.SizeOf throws an exception:


Type 'System.DateTimeOffset' cannot be marshaled as an unmanaged
structure; no meaningful size or offset can be computed.


I've given up on trying to calculate the size of a struct like this, because every line of attack/blog post comes up with one error or another.

Can someone just tell me what the average size of a DateTimeOffset is on a 64 bit Azure web server is?

Answer

It occupies 10 bytes.

Looking at the source, it has an Int16 and a DateTime; DateTime has a single UInt64.

I have no idea why Marshal.SizeOf can't measure it.