views:

424

answers:

9

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.)

+6  A: 

Because it is a single point. It doesn't have multiple pieces of data. Under the hood it is represented by a long.

Yuriy Faktorovich
Why the downvotes? I answered his original question.
Yuriy Faktorovich
I didn't down vote, but from your answer follow that structures are always contain a single "piece of data" and this is the only reason that DateTime is the structure. You said nothing about performance diference between structures and classes, which, I think, should be included to question's answer.
Kamarey
+6  A: 

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.

Alexander Gräf
+7  A: 

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.

rdkleine
Yes, but please don't name it "ToString2" ! :)
Dave Markle
Sure, its just an example... ;)
rdkleine
A: 

Yes, extension methods can sure be the way to go.

+4  A: 

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; }
    }
}
Wayne Hartman
+1  A: 
  • 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.

elder_george
+6  A: 

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.

Ruben
+1  A: 

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.

epotter
+2  A: 

You don't need to ovveride the ToString() method just check the patterns here you won't really need others :)

more detailed post here

Yassir