Why can't I inherit from DateTime?

  • Thread starter Thread starter Frank Rizzo
  • Start date Start date
Jeffrey,
As Bruce states the "instance size under 16 bytes" is a guide line (all four
are guidelines).
The DateTime struct, if you look at all the "long" members, its
instance size would be more than 16 bytes.
The size of a DateTime is a single Long. Don't confuse the size of various
properties with the underlying storage. The only real value stored is
DateTime.Ticks, the other properties are derived from DateTime.Ticks.

<quote>
Note Prior to the .NET Framework 2.0, a DateTime consisted solely of a ticks
field, which is a 62-bit unsigned field that contains the number of ticks
that represent the date and time. The value of the ticks field can be
obtained with the Ticks property.
</quote>

http://msdn2.microsoft.com/library/03ybds8y.aspx

Hope this helps
Jay
 
Jon,
You are right. It only has one instance field, a long. I didn't look
closely enough. All the others are static or constants.

I did find System.Diagnostics.CounterSample that has 7 long members (8
bytes per field). Am I right about that?

So this may be one time where the 16 bytes is exceeded, but I can see
the reason to keep the instance size as small as possible.

Best regards,
Jeffrey Palermo
Blog: http://www.jeffreypalermo.com
 
Jeffrey Palermo said:
You are right. It only has one instance field, a long. I didn't look
closely enough. All the others are static or constants.

I did find System.Diagnostics.CounterSample that has 7 long members (8
bytes per field). Am I right about that?

I think so.
So this may be one time where the 16 bytes is exceeded, but I can see
the reason to keep the instance size as small as possible.

Yup. I think the main point is to favour reference types in general.
 
A simple answer to your question is:
JUST BECAUSE MICROSOFT DOESN'T WANT YOU TO DO THAT!!!

Otherwise, they would have created a class called DateTime

Any ideas?



--
Regards,
Nurchi BECHED

P.S.
C makes it easy to shoot yourself in the foot;
C++ makes it harder, but when you do,
it blows away your whole leg."
--Bjarne Stroustrup
 
Back
Top