Why is DateTime
a structure instead of an inheritable class?
(I would like to be able to override the ToString() method but I can't.)
Why is DateTime
a structure instead of an inheritable class?
(I would like to be able to override the ToString() method but I can't.)
Because it is a single point. It doesn't have multiple pieces of data. Under the hood it is represented by a long.
If you want to know more about system classes and structs, download the free .NET reflector (http://www.red-gate.com/products/reflector/).
Anyway, if you want to override how DateTime is formatted, provide your own IFormatProvider. Use .NET Reflector to find out how DateTimeFormatInfo is implemented, and then implement your own.
You can use Extension Methods for this: Declare extension:
public static class DateTimeExtensions
{
public static string ToString2(this DateTime date)
{
return date.ToString("{d}");
}
}
Use extension:
var d = new DateTime();
System.Diagnostics.Debug.WriteLine(d.ToString2());
This way you can simple implement your own method thats used on DateTime. This way it can easily be used everywhere within you solution. Only thing you have to do is using the namespace.
Just because it is a struct (or even if it were a sealed class), that does not mean that it is the end of the road. You can solve this problem with composition, rather than inheritance. Here's an example of 'objectifying' the DateTime class:
public class MyDateTime
{
DateTime? value;
public MyDateTime()
{
this.Value = DateTime.Now;
}
public MyDateTime(DateTime? dateTime)
{
this.Value = dateTime;
}
public override String ToString()
{
if (this.Value != null)
{
return this.Value.Value.Month.ToString() + " my favorite time of the year";
}
return null;
}
public System.DateTime? Value
{
get { return this.value; }
set { this.value = value; }
}
}
DateTime has a small size. So it is not efficient to allocate memory at heap for this.
It is logical to make copying of it on assignment, not creating a reference to the same object, especially since it is immutable. The same is true for operation on it (Add
, Substract
etc.)
It had to be convertible to the corresponding COM structure.
If you want custom formatting you can implement it like it is shown here.
Probably because it was seen as a small, simple and immutable data structure, much like an integer or decimal. Making it a struct under these conditions make using a DateTime very efficient. If it had been made a class, this efficiency benefit would have been lost because that would require memory allocations every time you create a new DateTime.
Besides, how many variant forms of a DateTime can you come up with? (Ignoring the allternate ToString implementations you have in mind.) It's not exactly a type that invites polymorphism.
Note that for using a different formatting strategy for your DateTimes, as I think you want, you'd better look at a different way to format than just using ToString. If you look at the ICustomFormatter interface in MSDN, you'll see how you can plug into the String.Format pipeline to override formatting without needing to subset an existing type.
I believe that it is a struct because structs are value types and classes are reference types. The actual data in a DateTime is a single long integer. If it were a class, every time you created a new object, 8 bytes would be allocated on the heap and another 8 bytes would be allocated on the stack for the pointer. So by making a DateTime a struct, it effectively cuts the memory requirements in half.
You can find more information in this question.