Why does the System.DateTime struct have layout kind Auto?

前端 未结 1 1459
Happy的楠姐
Happy的楠姐 2021-01-04 03:00

The struct System.DateTime and its cousin System.DateTimeOffset have their structure layout kinds set to \"Auto\". This can be seen with:

typeof         


        
相关标签:
1条回答
  • 2021-01-04 03:45

    This is going to require speculation, this decision was made a long time ago, well before .NET 1.0 shipped. The attribute on System.DateTime is at best a micro-optimization, not uncommon in .NET code. It is somewhat appropriate, the struct has only one field so there's never any issue with layout. The ones for the internal CustomAttribute structs were probably done by the same programmer. Doesn't matter either, unmanaged code never sees them.

    The one for System.DateTimeOffset was done much later and almost certainly a copy-paste bug.

    That programmer got away with it, no reason for the CLR to re-arrange the layout from the sequential version. Re-arranging with auto-layout occurs when the struct contains padding between fields that is large enough to fit another small field. Not the case for DateTimeOffet.

    Some odds you'll get a Microsoft guru to pay attention to this when you file a feedback report for DateTimeOffset. It is wrong afaik. Post it to connect.microsoft.com

    0 讨论(0)
提交回复
热议问题