Why is DateTime sealed???

  • Thread starter Thread starter Programmer
  • Start date Start date
P

Programmer

What is the purpose of making DateTime sealed? Before C# came out, I had
made my own temporal class library in C++. I was going to convert it to C#
and inherit from DateTime (and delete about 2/3 of my own code in doing so)
which would make it more compatible with other DotNET code that expects
DateTime objects (ie. Polymorphism). Since MS sealed DateTime, I can't do
that. Argh!

Does anyone know the specific reasons of making that particular class
sealed?

Why would MS care if I change the "critical" behavior? If I do, and I break
my own derived class, that's *MY* problem, not theirs... at least, if it's
not sealed, I have the option of extending the behavior of a useful class
and have the capability of gaining the benefits of polymorphism.

What I'm interested in knowing is not a generic overview of the concept of
"sealed" but the specifics of why DateTime is sealed. It's really been a
huge hindrance to me. :(

Thanks!
 
It isn't necessarily that MS wants to stop you from extending DateTime, but
DateTime is defined as a Value Type. As such, it is not a reference type,
and can be placed directly on the stack (as opposed to being forced to the
heap) when you use a value type as a parameter or local variable.

No value type can be inherited. Only reference types can be inherited.

-Rob [MVP]
 
Back
Top