In the .Net framework DateTime structure, the Year is defined as an int (which is really A System.Int32). However, the MSDN documentation says that the value will always be between 1 and 9999. Thus, a ushort (System.UInt16) is more than adequate to store the value and takes up half the space. So why is it an int and not a ushort?
There is an implicit conversion from ushort to int so there is no casting that needs to be done to do integer arithmetic on the Year.
I realize this is a micro-optimization issue and thus not very important. I am just curious.
Thus, a ushort (System.UInt16) is more than adequate to store the value and takes up half the space.
Where do you think that "space" is being wasted? DateTime
doesn't store each component in a separate field anyway. If you're storing the year somewhere, feel free to cast it to a ushort
- and cast Month
to a byte
, etc.
Note that ushort
isn't CLS-compliant, which is probably the reason for it. There are a lot of properties which would make sense to be unsigned, such as string.Length
etc... but the framework tries to be CLS-compliant where it can.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With